依赖项属性绑定问题WPF

时间:2013-09-27 06:00:15

标签: c# wpf xaml dependency-properties

请让我知道我做错了什么

UserControl的XAML,我将我的孩子UserControl与依赖属性

一起使用

ComboBox 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>

我放置断点来检查依赖属性是否会在父用户控件上从ComboBox更新时受到影响。但不是......

1 个答案:

答案 0 :(得分:1)

您是否已尝试使用以下内容修改PropertyBinding中的ChildControl

"Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"

您还可以尝试为ComboBox提供一个名称并将ChildControl绑定到该名称:

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