C# 启动时选择列表中的第一个组合框项

C# 启动时选择列表中的第一个组合框项,c#,wpf,combobox,wpf-controls,C#,Wpf,Combobox,Wpf Controls,更新1 这不是关于“启动时选择第n项”。。。这更像是选择一个被定义为初始项的项目,然后更新组合框。我需要将ItemsSource设置为CompositeCollection,其中一个项定义为指定项(不必是项0),并在启动时将所提到的项设置为选中项。将集合绑定到项内容的事实在这里起着至关重要的作用。下面的代码只是演示了一个示例应用程序 更新1结束 我遇到了一个小问题,我希望我能在这里得到解决。我有一个组合框,我想用启动时选择的特定项目初始化它。问题是,当我启动应用程序时,控件为空,并在第一次打开时

更新1

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

更新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>
我试着让代码复制粘贴工作


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

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

XAML

   <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
 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));
    }
}

视图模型

   <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
 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));
    }
}
公共类视图模型:INotifyPropertyChanged
{
私有列表m_ComboItems=新列表();
私有字符串m_SelectedItem;
公共视图模型()
{
m_ComboItems.添加(“AA”);
m_组合项目。添加(“BB”);
m_ComboItems.添加(“CC”);
this.SelectedItem=m_ComboItems.First();
}
公共列表组合项
{
获取{return m_ComboItems;}
}
公共字符串SelectedItem
{
获取{return m_SelectedItem;}
设置
{
m_SelectedItem=值;
此.OnPropertyChanged(“SelectedItem”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的无效OnPropertyChanged(字符串propertyName)
{
if(this.PropertyChanged!=null)
this.PropertyChanged(this,newpropertychangedventargs(propertyName));
}
}
如果要将selectedItem更改为列表中第一项以外的内容,则需要将selectedItem设置为列表中的对象,例如selectedItem=m_ComboItems[1]将把“BB”作为所选项目


希望这有帮助

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

XAML

   <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
 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));
    }
}

视图模型

   <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
 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));
    }
}
公共类视图模型:INotifyPropertyChanged
{
私有列表m_ComboItems=新列表();
私有字符串m_SelectedItem;
公共视图模型()
{
m_ComboItems.添加(“AA”);
m_组合项目。添加(“BB”);
m_ComboItems.添加(“CC”);
this.SelectedItem=m_ComboItems.First();
}
公共列表组合项
{
获取{return m_ComboItems;}
}
公共字符串SelectedItem
{
获取{return m_SelectedItem;}
设置
{
m_SelectedItem=值;
此.OnPropertyChanged(“SelectedItem”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的无效OnPropertyChanged(字符串propertyName)
{
if(this.PropertyChanged!=null)
this.PropertyChanged(this,newpropertychangedventargs(propertyName));
}
}
如果要将selectedItem更改为列表中第一项以外的内容,则需要将selectedItem设置为列表中的对象,例如selectedItem=m_ComboItems[1]将把“BB”作为所选项目


希望这有帮助

如果希望在组合框的itemSource初始化后立即选择第一个项目,请将组合框的
IsSynchronizedWithCurrentItem
设置为true,如下所示-

<ComboBox IsSynchronizedWithCurrentItem="True"/>

如果您希望在组合框的itemSource初始化后立即选择第一个项目,请将组合框的
IsSynchronizedWithCurrentItem
设置为true,如下所示-

<ComboBox IsSynchronizedWithCurrentItem="True"/>


是否希望默认选择第一个项目?是的,我希望选择此特定项目。行为是,当我第一次启动应用程序打开组合框时,它的内容会被更新。好的,我看到你更改了标题-我想选择项目-在我的示例代码中,它是项目0,但可以是任何索引、值等。这个概念很重要-我选择了项目(它被选为属性,如果我选中SelectedItem,它就是正确的值)但是组合框的内容在我打开它之前不会得到更新…提示:似乎是内容的绑定设置问题…如果我调试它并在任何组合框上连接do Loaded事件,那么将SelectedItem作为ComboBoxItem并检查内容声明为空,但是如果我打开它,值会发生变化(绑定得到解决)我可能需要在那里玩绑定表达式…你想默认选择第一个项目吗?是的,我想选择这个特定的项目。行为是,当我第一次启动应用程序打开组合框时,它的内容就会更新。好的,我看到你在我的示例代码中更改了标题-我想选择项目它是项0,但可以是任何索引、值等。这个概念很重要-我选择了项(它被选为属性,如果我选中SelectedItem,它的值是正确的)但是组合框的内容在我打开它之前是不会更新的…提示:看起来像是内容上的绑定集…如果我调试它并在任何组合框上连接do Loaded事件,而不是将SelectedItem作为ComboBoxI