2013-09-27 196 views
0

请让我知道我在做什么的UserControl,我用我的孩子UserControl与依赖属性错误依赖属性绑定问题WPF

XAML

组合框SelectedItem将更新SelectedEmployee

SelectedEmployee是进一步绑定我的孩子控制CardView:uEmployeeCard ViewModel属性

<ComboBox Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" 
      Style="{StaticResource UiComboBox}" 
      ItemsSource="{Binding TB_EMPLOYEEList}" 
      SelectedItem="{Binding SelectedEmployee, 
          Mode=TwoWay, 
          UpdateSourceTrigger=PropertyChanged}" 
      SelectedValuePath="ID" 
      SelectedValue="{Binding CurrentTB_RECIEPT.REFRENCEID}" 
      ItemTemplate="{StaticResource ComboEmployeeTemplate}"/> 

<CardView:uEmployeeCard ViewModel="{Binding Path=SelectedEmployee}" 
         Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
         Grid.RowSpan="3" VerticalAlignment="Top"/> 

依赖项属性代码,代码背后uEmployeeCard的文件:

public uEmployeeCard() 
{ 
    InitializeComponent(); 
    this.DataContext = this; 
} 

public static readonly DependencyProperty ViewModelProperty = 
    DependencyProperty.Register("ViewModel", 
     typeof(TB_EMPLOYEE), 
     typeof(uEmployeeCard), new FrameworkPropertyMetadata 
      { 
       BindsTwoWayByDefault = true, 
       DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
      }); 

[Bindable(true)] 
public TB_EMPLOYEE ViewModel 
{ 
    get { return (TB_EMPLOYEE)GetValue(ViewModelProperty); } //do NOT modify anything in here 
    set { SetValue(ViewModelProperty, value); } //...or here 
} 

uEmployeeCard(儿童用户控制)的XAML文件:

<StackPanel Grid.Column="0" Grid.Row="0" Orientation="Horizontal"> 
    <TextBlock Style="{StaticResource UiTextBlock}" 
       Text="{Binding Path=ViewModel.TITLE}"/> 
    <TextBlock Style="{StaticResource UiTextBlock}" 
       Text="{Binding Path=ViewModel.FIRSTNAME}"/> 
    <TextBlock Style="{StaticResource UiTextBlock}" 
       Text="{Binding Path=ViewModel.FIRSTNAME}"/> 
</StackPanel> 
<StackPanel Grid.Column="0" Grid.Row="1" Orientation="Vertical" Grid.RowSpan="2"> 
    <TextBlock Style="{StaticResource UiTextBlock}" 
       Text="{Binding Path=ViewModel.NATNO}"/> 
</StackPanel> 

我p ut断点来检查依赖属性是否受到来自父级用户控件上ComboBox更新的影响。但不...

+0

WPF通常会绕过依赖属性的CLR包装,因此不会调用您的getter或setter。请参阅此处以获取解释:[XAML加载和依赖项属性](http://msdn.microsoft.com/zh-cn/library/bb613563.aspx)。 – Clemens

+0

是的你是对的,但正如你看到我没有逻辑。除此之外,我交叉检查了我的代码,但无法找到我丢失的内容,这样一旦我在我的父控件上更改了绑定了组合框的selecteditem子控件的更改,我的chilcontrols字段就不会填充。 –

+0

您是否已经尝试使用以下编辑ChildControl中的PropertyBinding:“Mode = TwoWay,UpdateSourceTrigger = PropertyChanged”?你也可以尝试给你的ComboBox一个名称,并将你的ChildControl绑定到ViewModel =“{Binding ElementName = NamedComboBox,Path = SelectedItem}” – Basti

回答

1

你有没有尝试过用下面编辑PropertyBindingChildControl

"Mode=TwoWay, UpdateSourceTrigger=PropertyChanged" 

您也可以尝试给你ComboBox名称和绑定你ChildControl它:

ViewModel="{Binding ElementName=NamedComboBox, Path=SelectedItem}"