简体   繁体   中英

Dependency property binding Issue WPF

Kindly let me know what I am doing wrong

XAML of UserControl where I use my child UserControl with dependency property

ComboBox SelectedItem will Update SelectedEmployee ,

This SelectedEmployee is further Binded with My child Control CardView:uEmployeeCard ViewModel Property

<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"/>

Dependency Property Code, Code Behind file of 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
}

Xaml File of uEmployeeCard (Child User Control):

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

I put breakpoint to check whether the dependency property get affected on update from ComboBox on parent user control. but not...

Have you already tried to edit the PropertyBinding in the ChildControl with the following:

"Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"

You could also try to give your ComboBox a name and bind your ChildControl to it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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