【问题标题】:Dependency property binding Issue WPF依赖属性绑定问题 WPF
【发布时间】:2013-09-27 06:00:14
【问题描述】:

请告诉我我做错了什么

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 更新时受到影响。但不是……

【问题讨论】:

  • WPF 通常会绕过依赖属性的 CLR 包装器,因此不会调用您的 getter 或 setter。请参阅此处了解说明:XAML Loading and Dependency Properties.
  • 是的,你是对的,但正如你所见,我没有把逻辑放在那里。除此之外,我交叉检查了我的代码,但找不到我缺少的内容,因此一旦我在绑定到组合框 selecteditem 子控件的父控件上进行更改,我的 chilcontrols 字段就不会被填充。
  • 您是否已经尝试使用以下内容编辑 ChildControl 中的 PropertyBinding:“Mode=TwoWay,UpdateSourceTrigger=PropertyChanged”?您也可以尝试为您的 ComboBox 命名,并将您的 ChildControl 绑定到 ViewModel="{Binding ElementName=NamedComboBox, Path=SelectedItem}"
  • @BastiOnWpf 是的,亲爱的你的代码工作,现在它的工作非常感谢。兄弟...我想将您的评论标记为答案。以便它也可以帮助其他人。

标签: c# wpf xaml dependency-properties


【解决方案1】:

您是否已经尝试使用以下内容编辑ChildControl 中的PropertyBinding

"Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"

您也可以尝试为您的 ComboBox 命名并将您的 ChildControl 绑定到它:

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

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 2012-11-08
    • 2013-01-31
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2023-03-26
    相关资源
    最近更新 更多