C# 组合框“选择项”绑定

C# 组合框“选择项”绑定,c#,wpf,binding,combobox,C#,Wpf,Binding,Combobox,我正在处理一些组合框,它们需要一个select属性作为wpfc中的顶级选项 目前,我已经命名了组合框,然后从数组字符串填充到代码中 <ComboBox Width="150" x:Name="cmbTitle" Margin="3" SelectedIndex="0" /> 我的问题是,选定的索引将始终被字符串中索引的1关闭 在做了研究之后,我发现这是一种从背景填充组合框的非常古老的方式。在我所看到的每个示例中,常量似乎都存储在枚举中,所以我希望从多个字符串[]中移开 在WPF中容

我正在处理一些组合框,它们需要一个select属性作为wpfc中的顶级选项

目前,我已经命名了组合框,然后从数组字符串填充到代码中

<ComboBox Width="150" x:Name="cmbTitle" Margin="3" SelectedIndex="0" />
我的问题是,选定的索引将始终被字符串中索引的1关闭

在做了研究之后,我发现这是一种从背景填充组合框的非常古老的方式。在我所看到的每个示例中,常量似乎都存储在枚举中,所以我希望从多个字符串[]中移开

在WPF中容纳select选项的同时,将枚举绑定到组合框的最佳方法是什么? 今天我看了半打选项,我不太确定还有哪些代码示例要列出

这是一个相当开放的问题,但我完全迷路了

提前感谢,,
奥利

我认为填充组合框的最佳方法是使用IDictionary

例如,您的代码隐藏:

public YourEnum SelectedOption { get; set; }

public IDictionary<string, YourEnum> Options = new Dictionary<string, YourEnum?>();

Options.Add("Select", null);
Options.Add("Option 1", YourEnum.Option1);
...
Options.Add("Option N", YourEnum.OptionN);
您的xaml文件:

<ComboBox ItemsSource="{Binding Options, ...}" SelectedValue="{Binding SelectedOption, ...}" DisplayMemberPath="Key" SelectedValuePath="Value" />
枚举的值可以从中检索,绑定到方法通常使用ObjectDataProvider完成。以下是获取所有BindingMode值的示例:

我们的控件需要一个新属性来提示:

public class ExtendedComboBox : ComboBox
{
    public static readonly DependencyProperty PromptProperty =
        DependencyProperty.Register("Prompt", typeof(string), typeof(ExtendedComboBox), new PropertyMetadata(string.Empty));

    public string Prompt
    {
        get { return (string)GetValue(PromptTextProperty); }
        set { SetValue(PromptTextProperty, value); }
    }
}
我们可以作弊一点,在控件中放置一个带有提示的文本块,并在选中某个项目时将其隐藏。为此,我们使用包含TextBlock的新控件重写控件的ControlTemplate。我从以下位置修改了模板:

综合起来,我们有:

<local:ExtendedComboBox Style="{StaticResource PromptedComboBox}" Prompt="Select an item" ItemsSource="{Binding Source={StaticResource BindingModes}}" />

选择选项是可供选择的,还是只是提示用户?如果是后者,则有@Joulukuusi,它应该只是给用户一个提示。谢谢,这看起来很有用。
<ComboBox ItemsSource="{Binding Source={StaticResource BindingModes}}" />
public class ExtendedComboBox : ComboBox
{
    public static readonly DependencyProperty PromptProperty =
        DependencyProperty.Register("Prompt", typeof(string), typeof(ExtendedComboBox), new PropertyMetadata(string.Empty));

    public string Prompt
    {
        get { return (string)GetValue(PromptTextProperty); }
        set { SetValue(PromptTextProperty, value); }
    }
}
<Style x:Key="PromptTextBlock" TargetType="{x:Type TextBlock}" >
    <Setter Property="Visibility" Value="Hidden" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding SelectedItem, RelativeSource={RelativeSource TemplatedParent}}" Value="{x:Null}">
            <Setter Property="Visibility" Value="Visible" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style x:Key="PromptedComboBox" TargetType="{x:Type local:ExtendedComboBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ExtendedComboBox}">
                <Grid>
                    <ToggleButton x:Name="DropDownToggle"
                                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  
                                  Margin="-1" HorizontalContentAlignment="Right"
                                  IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
                        <Path x:Name="BtnArrow" Height="4" Width="8" 
                              Stretch="Uniform" Margin="0,0,4,0"  Fill="Black"
                              Data="F1 M 300,-190L 310,-190L 305,-183L 301,-190 Z " />
                    </ToggleButton>
                    <ContentPresenter x:Name="ContentPresenter" Margin="6,2,25,2"
                                      Content="{TemplateBinding SelectionBoxItem}"
                                      ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                      ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}">
                    </ContentPresenter>
                    <TextBox x:Name="PART_EditableTextBox"
                             Style="{x:Null}"
                             Focusable="False"
                             Background="{TemplateBinding Background}"
                             HorizontalAlignment="Left" 
                             VerticalAlignment="Center" 
                             Margin="3,3,23,3"
                             Visibility="Hidden"
                             IsReadOnly="{TemplateBinding IsReadOnly}"/>
                        <Popup x:Name="PART_Popup" IsOpen="{TemplateBinding IsDropDownOpen}">
                            <Border x:Name="PopupBorder" 
                                    HorizontalAlignment="Stretch" Height="Auto" 
                                    MinWidth="{TemplateBinding ActualWidth}"
                                    MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                    BorderThickness="{TemplateBinding BorderThickness}" 
                                    BorderBrush="Black" Background="White" CornerRadius="3">
                                <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
                                    <ItemsPresenter/>
                                </ScrollViewer>
                            </Border>
                        </Popup>
                    <TextBlock Margin="4,3,20,3" Text="{Binding PromptText, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource PromptTextBlock}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
     </Setter>
 </Style>
<local:ExtendedComboBox Style="{StaticResource PromptedComboBox}" Prompt="Select an item" ItemsSource="{Binding Source={StaticResource BindingModes}}" />