使用触发器更改所选项目的ListBox数据模板时出现问题。
当选择ListBox控件中的项目时,我使用触发器交换数据模板,以便我可以显示其他控件,包括ComboBox。不幸的是,当数据模板切换到SelectedArticleDataTemplate时,ComboBox的数据绑定似乎不起作用。我绑定到ViewSodel中的ItemSource的Observable Collection,以及Selected Item的属性。 INotifyPropertyChanged在VieModel和相关模型中的所有属性上实现。
以下是代码片段:
<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>
出于测试目的,我在数据模板之外添加了以下代码,它运行良好:
<ComboBox Width="200" Margin="0,0,0,10"
ItemsSource="{Binding ScaleGroups}"
SelectedItem="{Binding SelectedGroup}" />
我错过了什么?
由于
修改
ListBox在ViewModel中具有对ObservableCollection文章(目标权重,容差等)的数据绑定。当用户选择文章时,我想让他们通过ComboBox选择要将文章传输到哪个Scale Group。 - 我希望这是有道理的。
<ListBox Width ="863" Margin="5" Height="422"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Articles}"
SelectedItem="{Binding SelectedArticle}"
ItemContainerStyle="{StaticResource ContainerStyle}"
Background="#FFEAF0FF" />
在我的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");
}
}
解答:
感谢Clemens坚持要求我。
以下更改完全不同:
<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" />
答案 0 :(得分:0)
感谢Clemens坚持要求我。
以下更改完全不同:
<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" />