Skip to main content
 首页 » 编程设计

c#之在启动时选择列表中的第一个 ComboBox 项

2024年02月23日19emanlee

更新 1

这不是关于“在启动时选择第 n 个项目”...它更像是选择坐在那里定义为初始项目的项目并更新组合框。我需要将 ItemsSource 设置为 CompositeCollection,其中一个项目被定义为指定的(不一定是项目 0)并设置在启动时选择的提到的项目。绑定(bind)设置为项目内容的事实在这里起着至关重要的作用。下面的代码只是演示了一个示例应用程序。

更新 1 结束

我遇到了一个小问题,希望能在这里找到解决方案。我有一个组合框,我想用启动时选择的特定项目来初始化它。问题是,当我启动应用程序控件时,它是空的,并且在第一次打开时获取它的值。我已经设法将有问题的代码提取为尽可能最简单的形式(尽可能多地排除变量),它看起来如下

窗口的 XAML 代码

<Window 
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" mc:Ignorable="d" 
xmlns:loc ="clr-namespace:WpfApplication1" 
x:Class="WpfApplication1.MainWindow" 
x:Name="Window" 
Title="MainWindow" 
Width="640" Height="480"> 
 
<StackPanel Orientation="Vertical"> 
    <StackPanel.Resources> 
        <ComboBoxItem x:Key="toSelectInitially" Content="{Binding Path=ActiveItem,   Mode=OneWay}"/> 
    </StackPanel.Resources> 
 
    <ComboBox SelectedIndex="0" 
              Height="30"> 
        <ComboBox.ItemsSource> 
            <x:Array Type="{x:Type ComboBoxItem}"> 
                <ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/> 
                <ComboBoxItem Content="AAA"/> 
                <ComboBoxItem Content="BBB"/> 
            </x:Array> 
        </ComboBox.ItemsSource> 
    </ComboBox> 
 
    <ComboBox SelectedItem="{StaticResource ResourceKey=toSelectInitially}" 
              Height="30" Loaded="ComboBox_Loaded"> 
        <ComboBox.ItemsSource> 
            <x:Array Type="{x:Type ComboBoxItem}"> 
                <StaticResource ResourceKey="toSelectInitially"/>  
                <ComboBoxItem Content="AAA"/> 
                <ComboBoxItem Content="BBB"/> 
            </x:Array> 
        </ComboBox.ItemsSource> 
    </ComboBox> 
 
    <ComboBox SelectedValue="{Binding Path=ActiveItem, Mode=OneWay}" 
              SelectedValuePath="Content" 
              Height="30"> 
        <ComboBox.ItemsSource> 
            <x:Array Type="{x:Type ComboBoxItem}"> 
                <ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/> 
                <ComboBoxItem Content="AAA"/> 
                <ComboBoxItem Content="BBB"/> 
            </x:Array> 
        </ComboBox.ItemsSource> 
    </ComboBox> 
 
</StackPanel> 
 
 
</Window> 

代码隐藏:

namespace WpfApplication1 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
        this.InitializeComponent(); 
        this.DataContext = new VMSimple(); 
    } 
} 
} 

简单 View 模型:

using System; 
using System.ComponentModel; 
 
namespace WpfApplication1 
{ 
class VMSimple : INotifyPropertyChanged 
{ 
    public VMSimple() 
    { 
        ActiveItem = string.Concat("Active Item : ", Guid.NewGuid().ToString()); 
    } 
 
    private string mActiveItem; 
    public string ActiveItem 
    { 
        get { return mActiveItem; } 
        private set 
        { 
            if (Equals(mActiveItem, value)) return; 
            mActiveItem = value; 
            OnPropertyChanged("ActiveItem"); 
        } 
    } 
 
    public event PropertyChangedEventHandler PropertyChanged; 
 
    private void OnPropertyChanged(string propertyName) 
    { 
        if (PropertyChanged != null) 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
} 

我已尝试使代码复制粘贴工作。

显然所有方法的行为都相同(选定值、索引、项目)。如果我将列表设置为 Items 而不是 ItemsSource,问题就会消失,但这不是一个选项。请记住,这是我尝试使用 CompositeCollection 的更复杂代码的简化表示,但我已将其替换为数组以检查这是否不会导致问题。

请您参考如下方法:

这是我如何设置第一个组合框项目的完整示例:

XAML

   <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/> 

View 模型

 public class ViewModel : INotifyPropertyChanged 
{ 
    private List<string> m_ComboItems= new List<string>(); 
    private string m_SelectedItem; 
 
    public ViewModel() 
    { 
        m_ComboItems.Add("AA"); 
        m_ComboItems.Add("BB"); 
        m_ComboItems.Add("CC"); 
        this.SelectedItem = m_ComboItems.First<string>(); 
    } 
 
    public List<string> ComboItems 
    { 
        get { return m_ComboItems;}             
    } 
 
    public string SelectedItem 
    { 
        get { return m_SelectedItem; } 
        set 
        { 
            m_SelectedItem = value; 
            this.OnPropertyChanged("SelectedItem"); 
        } 
    } 
 
    public event PropertyChangedEventHandler PropertyChanged; 
 
    protected void OnPropertyChanged(string propertyName) 
    { 
        if (this.PropertyChanged != null) 
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

如果您想将 selectedItem 更改为列表中第一项以外的其他内容,则需要将 SelectedItem 设置为列表中的对象,例如SelectedItem=m_ComboItems[1] 会将“BB”作为选中的项目。

希望对您有所帮助!