2

ヘッダープロパティを使用して、カスタムの「PageHeaderControl」UserControlを作成しています。

 public partial class PageHeaderControl: UserControl
 {
    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header",
                        typeof(string), typeof(PageHeaderControl),
                        new PropertyMetadata(""));

    public string Header
    {
        get { return GetValue(HeaderProperty) as string; }
        set { SetValue(HeaderProperty, value); }
    }

 }

そのコントロールのXAMLには、次のものがあります。

<sdk:Label Content="{Binding Header,Mode=TwoWay}" />

ここで問題があります。コントロールを作成するとき、それをバインドすると、これを行うためにのみ機能します。

<my:PageHeaderControl Header="This is my page header" />

そして、これを行うことはできません。ここでPageHeader、ヘッダー値を保持しているViewModelのプロパティは次のとおりです。

<my:PageHeaderControl Header="{Binding PageHeader,Mode=TwoWay}" />

私のプロパティが台無しになっているのではないかと思いましたが、これも機能します。

<TextBlock Text="{Binding PageHeader,Mode=TwoWay}" />

問題が何であるかについてのアイデア!

本当にありがとう!!!

編集:

私のViewModelでは、PageHeaderは次のとおりです。

private string _pageHeader = "This is my page header";
public string PageHeader
{
    get
    {
        return _pageHeader;
    }
    set
    {
        _pageHeader = value;
        RaisePropertyChanged("PageHeader");
    }
}

編集2:

プロパティの「get」内にブレークポイントを設定PageHeaderすると、TextBlockを追加しない限り、ブレークポイントはまったくヒットしません...

4

2 に答える 2

2

私があなたを正しく理解しているなら、あなたはあなたのコントロールのXAMLマークアップ内の要素のプロパティをコントロール自体のプロパティにバインドしようとしています。

この場合は、次のことが役立つかどうかを確認してください。

PageHeaderControl.xaml:

<UserControl x:Class="TryElementBinding.PageHeaderControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name = "MyControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <TextBlock Text="{Binding Header, ElementName=MyControl}"></TextBlock>
</Grid>

PageHeaderControl.xaml.cs:

public partial class PageHeaderControl : UserControl
{
    public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

    public string Header
    {
        get
        {
            return GetValue(HeaderProperty) as string;
        }
        set
        {
            SetValue(HeaderProperty, value);
        }
    }

    public PageHeaderControl()
    {
        InitializeComponent();
    }
}

ViewModel.cs:

public class ViewModel : INotifyPropertyChanged
{
    private string _pageHeader = "This is my page header";

    public string PageHeader
    {
        get
        {
            return _pageHeader;
        }
        set
        {
            _pageHeader = value;
            PropertyChanged(this, new PropertyChangedEventArgs("PageHeader"));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
}

MainPage.xaml:

<Grid x:Name="LayoutRoot" Background="White">
    <my:PageHeaderControl Header="{Binding PageHeader, Mode=TwoWay}"></my:PageHeaderControl>
</Grid>

MainPage.xaml.cs:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}
于 2012-09-07T18:45:09.763 に答える
0

私は少し混乱していて、Bindinginlineexpressionの構文を見逃したと思います。

「{Binding」の後にあなたのプロパティへのパスが来ます。「PageHeader」はあなたの財産への道ですか?!

私はあなたがこれを意味すると思います:

<my:PageHeader Header="{Binding PageHeader, Mode=TwoWay}" />

<TextBlock Text="{Binding PageHeader, Mode=TwoWay}" />

問題は、バインディング式が機能するのは、SetValueメソッドを使用してプロパティの値を設定し、特定のプロパティが変更されたことを親のDependencyObjectに通知した場合のみであるということです。DependencyPropertyを使用してTwoWayBindingを設定するか、クラスにSystem.ComponentModel.INotifyPropertyChangeインターフェイスを実装し、インターフェイスでPropertyChangedイベントを呼び出してBindingオブジェクトに手動で通知する必要があります。

PageHeaderプロパティの定義は次のようになります。

public static readonly DependencyProperty PageHeaderProperty = DependencyProperty.Register("PageHeader", typeof(string), typeof(YOUROWNER), new PropertyMetadata(""));

public string PageHeader
{
    get { return GetValue(PageHeaderProperty) as string; }
    set { SetValue(PageHeaderProperty, value); }
}

乾杯

于 2012-09-07T13:54:20.583 に答える