wpf ComboBox绑定数据

gz5pxeao  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(141)

你能帮我解决我第二天遇到的问题吗?我想把我的源代码中的数据绑定到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>

bzzcjhmw

bzzcjhmw1#

没有理由为ComboBox使用样式,你可以只设置属性值。
目前LineTypes是静态的,常用的绑定语法不适用,但可以使用{x:Static }扩展:

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

字符串

相关问题