【问题标题】:Select Item of static ComboBoxItems in WPF在 WPF 中选择静态 ComboBoxItems 的项目
【发布时间】:2015-11-20 07:49:58
【问题描述】:

我有一个带有静态项目的组合框

<ComboBox SelectedItem="{Binding SelectedOperator}" >
     <ComboBoxItem Content="=" IsSelected="True" />
     <ComboBoxItem Content="&gt;" />
     <ComboBoxItem Content="&lt;" />                    
</ComboBox>

虽然设置了IsSelected="True",但第一项没有被选中。

我可以在 WPF 中以某种方式选择它吗?如果真的有必要,我只想在代码隐藏中进行。

【问题讨论】:

  • 就个人而言,我想我只需在您的 ViewModel 或代码隐藏中创建一个 ObservableCollection,用您的运算符填充它,然后将 ComboBox 的 ItemsSource 设置为集合。请参阅:stackoverflow.com/a/19632600/1095741 了解类似方法。

标签: c# wpf mvvm combobox


【解决方案1】:

您可能希望将 ComboBoxItem 的 Content 字符串用于 SelectedOperator 绑定,在这种情况下,您可以使用 FallbackValue:

<ComboBox SelectedValuePath="Content"
          SelectedValue="{Binding SelectedOperator, FallbackValue=\=}">
    <ComboBoxItem Content="=" />
    <ComboBoxItem Content="&gt;" />
    <ComboBoxItem Content="&lt;" />
</ComboBox>

【讨论】:

  • 它与在我的 ViewModel 构造函数中设置 SelectedOperator 结合使用。
【解决方案2】:

如果您绝对必须将所有内容保存在 XAML 中,则命名 ComboBox 并从其他元素绑定到其 SelectedItem 可能是可行的。您不会绑定到 SelectedOperator 以在您的 VM/代码隐藏中使用,但这可能不会破坏交易,具体取决于您的应用程序的设置方式。

<Grid x:Name="LayoutRoot" HorizontalAlignment="Center">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox x:Name="MyComboBox"
            Grid.Row="0"
            Width="80"
            Height="25">
        <ComboBoxItem Content="=" IsSelected="True" />
        <ComboBoxItem Content="&gt;" />
        <ComboBoxItem Content="&lt;" />
    </ComboBox>
    <TextBox Grid.Row="1" Text="{Binding ElementName=MyComboBox, Path=SelectedItem.Content}" />
</Grid>

【讨论】:

    【解决方案3】:

    你的ComboBox会如何选择SelectedItem?

    您将SelectedItem 设置为"{Binding SelectedOperator}",同时尝试将第一项的IsSelected 属性设置为True。

    任意设置SelectedItemBinding

    <ComboBox SelectedItem="{Binding SelectedOperator}" >
         <ComboBoxItem Content="=" />
         <ComboBoxItem Content="&gt;" />
         <ComboBoxItem Content="&lt;" />                    
    </ComboBox>
    

    或者设置IsSelected属性:

    <ComboBox>
         <ComboBoxItem Content="=" IsSelected="True" />
         <ComboBoxItem Content="&gt;" />
         <ComboBoxItem Content="&lt;" />                    
    </ComboBox>
    

    【讨论】:

    • 这两种方法都可以正常工作 - 如果您使用第一种方法,您需要在 ViewModel/codebehind 中的某处设置 SelectedOperator,并确保您已实现 INotifyPropertyChanged。如果您使用第二种方法,它应该选择 '=' 选项,但它不会设置 SelectedOperator,因为没有绑定。
    • @goobering:你试过了吗?第二个选项不会选择 GUI 中的项目。
    【解决方案4】:

    希望这会有所帮助。

        <ComboBox Margin="53,119,35,157"
                  SelectedValue="{Binding SelectedOperator}"
                  SelectedValuePath="Content">
            <ComboBoxItem Content="=" />
            <ComboBoxItem Content="&gt;" />
            <ComboBoxItem Content="&lt;" />
        </ComboBox>
    

    代码背后:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            var v = new ViewModel();
            this.DataContext = v;
        }
     }
    

    视图模型:

    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            SelectedOperator = "=";
        }
    
        private string _selectedOperator;
    
        public string SelectedOperator
        {
            get { return _selectedOperator; }
            set { _selectedOperator = value; OnPropertyChanged(); }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2014-05-17
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多