ComboBox 绑定数据

ComboBox binding data

提问人:Daniel 提问时间:11/16/2023 最后编辑:Daniel 更新时间:11/16/2023 访问量:57

问:

你能帮我解决我第二天遇到的问题吗? 我想将源代码中的数据绑定到 WPF ComboBox 中,我正在尝试这样做:

WPF 窗口

<mah:MetroWindow x:Class="AlarmConfiguration.Lines.LineSettingsRadio"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AlarmConfiguration.Lines"
        xmlns:resx="clr-namespace:AlarmConfiguration.Resources" d:DataContext="{d:DesignInstance Type=local:Line}"
        mc:Ignorable="d"
        Title="{x:Static resx:Translations.RemoteConfig}" Height="220" Width="320" Closed="MetroWindow_Closed">
    
    <StackPanel Orientation="Vertical">
        <CheckBox Content="Linia 24h"/>
        <ComboBox SelectedItem="{Binding Type, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}">
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="ItemsSource" Value="{Binding LineTypes}" />
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
        </ComboBox>
    </StackPanel>   
</mah:MetroWindow>

文件和命名空间行

        [MarshalAs(UnmanagedType.I1)] public LineType lineType = LineType.NotUsed;

        public string Type
        {
            get => Converter.EnumDescription.GetDescription(thisLine.lineType);
            set
            {
                foreach (var lineType in Enum.GetValues(typeof(LineType)).Cast<LineType>())
                {
                    if (Converter.EnumDescription.GetDescription(lineType) == value)
                    {
                        thisLine.lineType = lineType;
                        break;
                    }
                }
                OnPropertyChanged();
            }
        }

        public static IEnumerable<string> LineTypes
        {
            get
            {
                yield return Converter.EnumDescription.GetDescription(LineType.NotUsed);
                foreach (var lineType in Enum.GetValues(typeof(LineType)).Cast<LineType>())
                {
                    if(lineType != LineType.NotUsed)
                        yield return Converter.EnumDescription.GetDescription(lineType);
                }
            }
        }

        public enum LineType : byte
        {
            [LocalizedDescription("LineTypeEntryExit1", typeof(Translations))] Enter_Exit_1 = 0,
            [LocalizedDescription("LineTypeEntryExit2", typeof(Translations))] Enter_Exit_2 = 1,
            [LocalizedDescription("LineTypeInternal", typeof(Translations))] Internal = 2,
            [LocalizedDescription("LineTypeInstant", typeof(Translations))] Instant = 3,
            //[LocalizedDescription("LineTypeJointly", typeof(Translations))] Jointly = 4,
            [LocalizedDescription("LineTypeNotUsed", typeof(Translations))] NotUsed = 5,
            [LocalizedDescription("LineTypeFire24", typeof(Translations))] Fire24 = 6,
            [LocalizedDescription("LineTypeBreakingIn24", typeof(Translations))] BreakingIn24 = 7,
        }

结果,我在 comboBox 中有 System.Windows.Style 文本,当我在那里设置断点时,代码不会进入 LineTypes getter

为了进行比较,有类似的对象使用相同的 LineTypes 并正常工作。这是另一个文件中的示例:

                <DataGridComboBoxColumn Header="{x:Static resx:Translations.ZoneAudiableType}" SelectedItemBinding="{Binding Sound, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" Width="150">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="VerticalAlignment" Value="Center" />
                            <Setter Property="ItemsSource" Value="{Binding SoundTypes}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding SoundTypes}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
C# XML WPF

评论


答:

1赞 ASh 11/16/2023 #1

没有理由对 ComboBox 使用 Style,您可以只设置属性值。

目前是静态的,常用的绑定语法不适用,但可以使用 {x:Static } 扩展名:LineTypes

<ComboBox SelectedItem="{Binding Type, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"
          VerticalAlignment="Center"
          ItemsSource="{x:Static local:Line.LineTypes}"/>

评论

0赞 Daniel 11/16/2023
谢谢。它帮助我解决了这个问题。