[英]WPF ViewModel Data Binding not working when Changing a ListBox's Data Template via a Trigger
I am having an issue when using a Trigger to change a ListBox's Data Template for the selected item. 使用触发器更改所选项目的ListBox的数据模板时出现问题。
When an item in the ListBox control is selected, I use a trigger to swap out the Data Template so that I can show additional controls, including a ComboBox. 当选择ListBox控件中的项时,我使用触发器交换数据模板,以便我可以显示其他控件,包括ComboBox。 Unfortunately when the Data Template switches to the SelectedArticleDataTemplate, the ComboBox's Data Bindings do not seem to work. 不幸的是,当数据模板切换到SelectedArticleDataTemplate时,ComboBox的数据绑定似乎不起作用。 I am Binding to an Observable Collection in the ViewModel for the ItemSource, as well as a property for the Selected Item. 我绑定到ViewSodel中的ItemSource的Observable Collection,以及Selected Item的属性。 INotifyPropertyChanged is implemented on all Properties in the VieModel and associated Models. INotifyPropertyChanged在VieModel和相关模型中的所有属性上实现。
Here is a snippet of the code: 以下是代码片段:
<DataTemplate x:Key="ArticleDataTemplate">
<TextBlock Text="{Binding Model.ArticleName}" FontSize="12" Margin="5" />
</DataTemplate>
<DataTemplate x:Key="SelectedArticleDataTemplate">
<StackPanel Margin="150,0,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Select Scale Group :" Width="150" />
<ComboBox Width="200" ItemsSource="{Binding ScaleGroups}" SelectedItem="{Binding SelectedGroup}" Margin="0,0,0,10" />
</StackPanel>
</StackPanel>
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ArticleDataTemplate}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource SelectedArticleDataTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
For testing purposes, I added the following code outside of the Data Template and it worked perfectly: 出于测试目的,我在数据模板之外添加了以下代码,它完美地运行:
<ComboBox Width="200" Margin="0,0,0,10"
ItemsSource="{Binding ScaleGroups}"
SelectedItem="{Binding SelectedGroup}" />
What am I missing? 我错过了什么?
Thanks 谢谢
EDIT: 编辑:
The ListBox has a Data Binding to an ObservableCollection of Articles (Target Weights, Tolerances etc.) in the ViewModel. ListBox具有对ViewModel中的ObservableCollection文章(目标权重,容差等)的数据绑定。 When a user selects an article, I want to let them choose, via the ComboBox, which Scale Group to transmit the article to. 当用户选择文章时,我想让他们通过ComboBox选择要将文章传输到哪个Scale Group。 - I hope that makes sense. - 我希望这是有道理的。
<ListBox Width ="863" Margin="5" Height="422"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Articles}"
SelectedItem="{Binding SelectedArticle}"
ItemContainerStyle="{StaticResource ContainerStyle}"
Background="#FFEAF0FF" />
In my MainViewModel I have the following properties: 在我的MainViewModel中,我有以下属性:
/// <summary>
/// List of Scale Groups
/// </summary>
public ObservableCollection<string> ScaleGroups
{
get { return scaleGroups; }
set
{
scaleGroups = value;
RaisePropertyChanged("ScaleGroups");
}
}
/// <summary>
/// Selected Scale Group
/// </summary>
public string SelectedGroup
{
get { return selectedGroup; }
set
{
selectedGroup = value;
RaisePropertyChanged("SelectedGroup");
}
}
/// <summary>
/// List of Articles
/// </summary>
public ObservableCollection<ArticleViewModel> Articles
{
get { return articles; }
set
{
articles = value;
ApplyCollectionFilter();
RaisePropertyChanged("Articles");
}
}
/// <summary>
/// Selected Article
/// </summary>
public ArticleViewModel SelectedArticle
{
get { return selectedArticle; }
set
{
selectedArticle = value;
RaisePropertyChanged("SelectedArticle");
}
}
ANSWER: 回答:
Thanks to Clemens for persisting with me. 感谢Clemens坚持和我在一起。
The following change made all the difference: 以下更改完全不同:
<ComboBox Width="250"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.ScaleGroups}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.SelectedGroup}"
IsSynchronizedWithCurrentItem="True" Margin="0,0,0,10" />
Thanks to Clemens for persisting with me. 感谢Clemens坚持和我在一起。
The following change made all the difference: 以下更改完全不同:
<ComboBox Width="250"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.ScaleGroups}"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.SelectedGroup}"
IsSynchronizedWithCurrentItem="True" Margin="0,0,0,10" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.