2013-10-09 209 views
4

我得到了一些自定義依賴屬性綁定的問題。 我們:一個依賴屬性 自定義的用戶控制和約束自己:自定義依賴屬性綁定

<UserControl x:Class="WpfApplication1.SomeUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"> 
<Grid> 
    <Label> 
     <Label.Template> 
      <ControlTemplate> 
       <Label Content="{Binding MyTest}"/> 
      </ControlTemplate> 
     </Label.Template> 
    </Label> 
</Grid> 

...和控制代碼:

public partial class SomeUserControl : UserControl 
{ 
    public SomeUserControl() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty MyTestProperty = DependencyProperty.Register("MyTest", typeof(int), typeof(SomeUserControl)); 

    public int MyTest 
    { 
     get { return (int)GetValue(MyTestProperty); } 
     set { SetValue(MyTestProperty, value); } 
    } 
} 

我試圖用這個控制綁定簡單模型類的一些簡單屬性:

<UserControl x:Class="WpfApplication1.AnotherUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <wpfApplication1:SomeUserControl MyTest="{Binding Path=Model.MyNum}" Grid.Column="0"/> 
    <Label Content="{Binding Path=Model.MyNum}" Grid.Column="1"/> 
</Grid> 

...用代碼

public partial class AnotherUserControl : UserControl 
{ 
    public MyModel Model { get; set; } 
    public AnotherUserControl() 
    { 
     Model = new MyModel(); 
     Model.MyNum = 1231; 
     InitializeComponent(); 
    } 
} 

...和型號:

public class MyModel:INotifyPropertyChanged 
{ 
    private int _myNum; 

    public int MyNum 
    { 
     get { return _myNum; } 
     set { _myNum = value; OnPropertyChanged("MyNum");} 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

但結合不工作。堆棧在編譯時沒有錯誤。現在,綁定使用statndart wpf標籤控件(使用相同的模型),並且不使用我的自定義控件和自定義屬性。請幫助我理解這個問題的原因並解決它; 感謝)

回答

6

您應該在SomeUserControl中使用ElementName綁定。

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

<Grid> 
    <Label> 
     <Label.Template> 
      <ControlTemplate> 
       <Label Content="{Binding MyTest, ElementName=uc}"/> 
      </ControlTemplate> 
     </Label.Template> 
    </Label> 
</Grid> 

這裏有一些更多的信息,爲什麼你不應該將usercontrol的datacontext設置爲self/this。 Data Binding in WPF User Controls

2

你需要更新你的下面結合,即你必須結合使用的RelativeSource爲您設置的DataContext的祖先控件屬性孩子的用戶控件明確,默認綁定將用於路徑搜索子用戶控件的內部DataContext的。

 <wpfApplication1:SomeUserControl MyTest="{Binding Path=DataContext.Model.MyNum, RelativeSource={RelativeSource FindAncestor={x:Type UserControl}}}" Grid.Column="0"/> 
+0

不,這沒有用。控件模板中的綁定是正確的。因爲如果我將值分配給「MyTest」而沒有綁定,那麼控制工作正確。沒有從另一個地方工作的外部綁定。但我認爲在ControlTemplate級別上沒問題。 – kirilljk

+0

我的壞..沒有看到你明確設置childcontrol的datacontext自己..更新的答案應該工作 – Nitin

+0

它的工作。謝謝)但是有可能避免RelativeSource聲明? – kirilljk