繁体   English   中英

依赖属性绑定问题WPF

[英]Dependency property binding Issue WPF

请让我知道我在做什么错

我将子UserControl与依赖项属性一起使用的UserControl XAML

ComboBox SelectedItem将更新SelectedEmployee

SelectedEmployee与My child Control 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更新时受到影响。 但不是...

您是否已经尝试使用以下方法在ChildControl编辑PropertyBinding

"Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"

您也可以尝试给ComboBox命名,并将ChildControl绑定到该名称:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM