无法使用 DataTrigger 在 Combobox 中设置 SelectedValue

发布于 2024-11-14 01:32:47 字数 2267 浏览 1 评论 0原文

我有一个组合框,它有一个数据触发器,它根据虚拟机中的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-11-21 01:32:47

更改 ComboBox 绑定以使用 SelectedValue 而不是 SelectedPath。当值更改时,这将正确设置 IFBandwidth 视图模型属性。

触发器究竟有何用途?将调制属性更改为这样可能是更好的选择......

public TMod Modulation
{
    get { return modulation_; }
    set
    {
        modulation_ = value; 
        NotifyPropertyChanged("Modulation");

        if( modulation == TMod.P25 )
        {
            IFBandwith = TBand.Wide;
        }
    }
 }

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...

public TMod Modulation
{
    get { return modulation_; }
    set
    {
        modulation_ = value; 
        NotifyPropertyChanged("Modulation");

        if( modulation == TMod.P25 )
        {
            IFBandwith = TBand.Wide;
        }
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文