无法使用 DataTrigger 在 Combobox 中设置 SelectedValue
我有一个组合框,它有一个数据触发器,它根据虚拟机中的 .NET 属性值设置其 SelectedIndex。我的问题是我无法让设置器设置选定索引。
ItemSource 基于枚举数组。 窗口的 DataContext 是具有调制和带宽属性的 VM。
我是 WPF 新手,所以我确信我没有正确理解绑定,但我正在抓狂!提前感谢您的帮助。
这是风格。
<Style x:Key="BWCombBoxStyle" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
<DataTrigger
Binding="{Binding Modulation}" Value="P25">
<Setter Property="SelectedIndex" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
这是组合框:
<ComboBox Name="bandwidth"
Height="Auto" Width="70"
Style="{StaticResource BWCombBoxStyle}"
ItemsSource="{Binding BandwidthOptions, Mode=OneWay, ValidatesOnDataErrors=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding IFBandwidth, Mode=TwoWay, ValidatesOnDataErrors=True,
NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"/>
这是我的虚拟机中的 .Net 属性:
public TMod Modulation
{
get { return modulation_; }
set { modulation_ = value; NotifyPropertyChanged("Modulation"); }
}
public Channel.TBnd IFBandwidth
{
get { return chan_.IFBandwidth; }
set
{
chan_.IFBandwidth = value;
NotifyPropertyChanged("IFBandwidth");
}
}
public Channel.TBnd[] BandwidthOptions
{
get
{
return (Channel.TBnd[])System.Enum.GetValues(typeof(Channel.TBnd));
}
}
这是枚举:
public enum TMod
{
FM = 0,
AM = 1,
P25 = 2,
TRK = 3
}
public enum TBnd
{
Std = 0,
Nar = 1,
Wide = 2,
XWide = 3
}
I have a comboBox that has a datatrigger that set its SelectedIndex based on a .NET Property's value that in the VM. My problem is that I can't get the setter to set the Selected Index.
The ItemSource is based on a enum array.
The DataContext of the Window is the VM which has the Modulation, and Bandwidth properties.
I'm new to WPF so I'm sure I'm not understanding binding correctly, but I'm pulling my hair out! Thanks for your help in advance.
Here's the Style.
<Style x:Key="BWCombBoxStyle" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
<DataTrigger
Binding="{Binding Modulation}" Value="P25">
<Setter Property="SelectedIndex" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
Here's the ComboBox:
<ComboBox Name="bandwidth"
Height="Auto" Width="70"
Style="{StaticResource BWCombBoxStyle}"
ItemsSource="{Binding BandwidthOptions, Mode=OneWay, ValidatesOnDataErrors=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding IFBandwidth, Mode=TwoWay, ValidatesOnDataErrors=True,
NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"/>
Here are the .Net Properties in my VM:
public TMod Modulation
{
get { return modulation_; }
set { modulation_ = value; NotifyPropertyChanged("Modulation"); }
}
public Channel.TBnd IFBandwidth
{
get { return chan_.IFBandwidth; }
set
{
chan_.IFBandwidth = value;
NotifyPropertyChanged("IFBandwidth");
}
}
public Channel.TBnd[] BandwidthOptions
{
get
{
return (Channel.TBnd[])System.Enum.GetValues(typeof(Channel.TBnd));
}
}
Here are the enums:
public enum TMod
{
FM = 0,
AM = 1,
P25 = 2,
TRK = 3
}
public enum TBnd
{
Std = 0,
Nar = 1,
Wide = 2,
XWide = 3
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改 ComboBox 绑定以使用 SelectedValue 而不是 SelectedPath。当值更改时,这将正确设置 IFBandwidth 视图模型属性。
触发器究竟有何用途?将调制属性更改为这样可能是更好的选择......
Change your ComboBox binding to use SelectedValue instead of SelectedPath. That will properly set the IFBandwidth view model property when the value is changed.
What exactly is the trigger going to be used for? It may be a better option to change your Modulation property to be something like this...