Reputation: 97
I have a reusable UserControl for selecting language using combobox with DependencyProperty SelectedCulture. Then I have another control with list of users displayed in a DataGrid using UsersViewModel containing collection of UserViewModel. One column is a DataGridTemplateColumn containing the language select control. I bound the property SelectedCulture to property of the UserViewModel, but the binding is not updating the value of the UserViewModel unless UpdateSourceTrigger is set to PropertyChanged.
Why not? Shouldn't be the PropertyChanged default value?
LanguageSelectView:
<UserControl x:Class="MyControls.LanguageSelectView" ... >
...
<ComboBox
ItemsSource="{Binding ViewModel.AvailableCultures, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LanguageSelectView}}}"
SelectedItem="{Binding SelectedCulture, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LanguageSelectView}}}"
>
<ComboBox.ItemTemplate>
...
</ComboBox.ItemTemplate>
</ComboBox>
</UserControl>
Code-behind:
public static readonly DependencyProperty SelectedCultureProperty = DependencyProperty.Register(nameof(SelectedCulture), typeof(CultureInfo),
typeof(LanguageSelectView), new FrameworkPropertyMetadata(null) {BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged});
public CultureInfo SelectedCulture
{
get { return (CultureInfo) GetValue(SelectedCultureProperty); }
set { SetValue(SelectedCultureProperty, value); }
}
[Import]
public LanguageSelectViewModel ViewModel { get; set; }
UsersView:
...
<DataGrid ItemsSource="{Binding Users}" AutoGenerateColumns="False" Name="DataGrid" SelectionChanged="DataGrid_OnSelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn Header="{wpf:Localize PreferredLanguage}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<localization:LanguageSelectView SelectedCulture="{Binding SelectedCulture}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
...
UserViewModel:
public class UserViewModel
{
...
public CultureInfo SelectedCulture
{
get { <Getter> }
set { <Setter> <-- It's not getting called! }
}
}
When I update the binding with UpdateSourceTrigger=PropertyChanged, everything works as expected. I checked, whether the DependencyProperty is being updated by the language control by using PropertyChangedCallback and it was working without problems, so it looks like problem with binding on the side of the UsersView.
Thanks!
Upvotes: 1
Views: 647