在 WPF 中执行静态组合框的有效方法



我的 wpf 应用程序中有一个静态ComboBox,它加载空间后跟 0-9。我有以下代码,它可以完成我需要的工作,但我觉得这不是一个好方法。任何建议或意见将不胜感激。

Test.xaml

<ComboBox Name="cbImportance" 
          Text="{Binding SelectedStory.ImportanceList, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" 
          Loaded="cbImportance_Loaded"
          Grid.Column="1"
          d:LayoutOverrides="Height" 
          Grid.ColumnSpan="2" 
          HorizontalAlignment="Stretch"
          Margin="0,9" 
          SelectionChanged="cbImportance_SelectionChanged" />

Test.xaml.cs

private void cbImportance_Loaded(object sender, RoutedEventArgs e)
{
    List<string> data = new List<string>();
    data.Add("");
    data.Add("0");
    data.Add("1");
    data.Add("2");
    data.Add("3");
    data.Add("4");
    data.Add("5");
    data.Add("6");
    data.Add("7");
    data.Add("8");
    data.Add("9");
    // ... Get the ComboBox reference.
    var cbImportance = sender as ComboBox;
    // ... Assign the ItemsSource to the List.
    cbImportance.ItemsSource = data;
    // ... Make the first item selected.
    cbImportance.SelectedIndex = 0;
}

哪一个是将静态值加载到ComboBox的有效方法:

  1. 通过XAML(由Anatoliy Nikolaev建议)
  2. xaml.cs(如上)
  3. ViewModel中创建构造函数并将静态值加载回ComboBox

在 WPF 中,通过标记扩展支持 XAML 中的对象数组。这对应于 x:ArrayExtension XAML 类型MSDN

您可以这样做:

<Window ...
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
<x:Array x:Key="ParametersArray" Type="{x:Type sys:String}">
    <sys:String>0</sys:String>
    <sys:String>1</sys:String>
    <sys:String>2</sys:String>
    <sys:String>3</sys:String>
    ...
</x:Array>
<ComboBox Name="ParameterComboBox"
          SelectedIndex="0"
          ItemsSource="{StaticResource ParametersArray}" ... />

要在x:Array中设置空字符串,请使用static成员:

<x:Array x:Key="ParametersArray" Type="{x:Type sys:String}">
    <x:Static Member="sys:String.Empty" />
    <sys:String>0</sys:String>
    <sys:String>1</sys:String>
    <sys:String>2</sys:String>
    <sys:String>3</sys:String>
    ...
</x:Array>

如果需要使用数字Int32类型定义静态ComboBox,它可以更短:

<Window.Resources>
    <Int32Collection x:Key="Parameters">0,1,2,3,4,5,6,7,8,9</Int32Collection>
</Window.Resources>
<ComboBox Width="100" 
          Height="30" 
          ItemsSource="{StaticResource Parameters}" />

或者像这样:

<ComboBox Width="100" Height="30">
    <ComboBox.ItemsSource>
        <Int32Collection>0,1,2,3,4,5,6,7,8,9</Int32Collection>
    </ComboBox.ItemsSource>
</ComboBox>
有许多

方法可以使用可枚举对象作为 ItemsSource,基于 {StaticResource}{Binding} 或其他依赖项属性。绑定是首选方式,因为它符合 MVVM 方法,建议用于 WPF 应用。

MVVM 方式

通过绑定,我们将值列表作为 ViewModel 对象的属性,该对象根据 WPF 设置为DataContext。所以,这里有一个绑定的例子

视图模型:

public class MyViewModel : INotifyPropertyChanged
{
    public ObservableCollection<SomeObject> Items
    {
        get { /* ... */ }
    }
    public SomeObject SelectedItem
    {
        get { /* ... */ }
        set { /* ... */ }
    }
}

视图:

<Window
    x:Class="Project1.MainWindow">
    <ComboBox
        ItemsSource={Binding Items}
        SelectedItem={Binding SelectedItem} />
</Window>

字符串数组作为静态资源

@AnatoliyNikolaev在之前的回答中对这个解决方案进行了很好的说明。

XmlDataProvider

有时您必须对具有可见标题的复杂值进行操作。将其放在 ViewModel 中是没有意义的 - 最好使用 XAML 功能。这就是它的样子:

<Window>
    <Window.Resources>
        <XmlDataProvider x:Key="YesNo" XPath="Items">
            <x:XData>
                <Items xmlns="">
                    <Item Header="Yes" Value="1"/>
                    <Item Header="No" Value="0"/>
                </Items>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid>
        <ComboBox
            ItemsSource="{Binding Source={StaticResource YesNo},XPath=*,Mode=OneTime}"
            SelectedValue="{Binding Value,UpdateSourceTrigger=PropertyChanged}"
            SelectedValuePath="@Value"
            DisplayMemberPath="@Header">
    </Grid>
</Window>

最新更新