通过触发器更改列表框的数据模板时,WPF ViewModel 数据绑定不起作用
WPF ViewModel Data Binding not working when Changing a ListBox's Data Template via a Trigger
我在使用触发器更改所选项目的列表框数据模板时遇到问题。
选择 ListBox 控件中的项目后,我使用触发器换出数据模板,以便可以显示其他控件,包括 ComboBox。不幸的是,当数据模板切换到 SelectedArticleDataTemplate 时,ComboBox 的数据绑定似乎不起作用。我绑定到 ItemSource 的 ViewModel 中的一个 Observable 集合,以及一个 属性 的 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" />
感谢 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" />
我在使用触发器更改所选项目的列表框数据模板时遇到问题。
选择 ListBox 控件中的项目后,我使用触发器换出数据模板,以便可以显示其他控件,包括 ComboBox。不幸的是,当数据模板切换到 SelectedArticleDataTemplate 时,ComboBox 的数据绑定似乎不起作用。我绑定到 ItemSource 的 ViewModel 中的一个 Observable 集合,以及一个 属性 的 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" />
感谢 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" />