【问题标题】:Default value for Combo box in WPF MVVMWPF MVVM 中组合框的默认值
【发布时间】:2014-03-17 22:24:59
【问题描述】:

我正在使用下面的代码在 ComboBox 中显示 num,

但默认文本 3 未显示。请帮助.....

请帮忙.....

等待您的答复。

<StackPanel  Orientation="Horizontal" Grid.Row="1">
<TextBlock Text="Width " VerticalAlignment="Center" Width="42" Margin="2,0,0,0"/>
<ribbon:ComboBox VerticalAlignment="Center" Text="3"  SelectedItem="{Binding SetWidth}"    Width="50" MinHeight="20" Margin="0,1,0,0">
    <ComboBoxItem Tag="1" Content="1" />
    <ComboBoxItem Tag="2" Content="2"/>
    <ComboBoxItem Tag="3" IsSelected="True" Content="3"/>
    <ComboBoxItem Tag="4" Content="4"/>
</ribbon:ComboBox>
</StackPanel>    

代码后面:

private ComboBoxItem _setWidth = new ComboBoxItem();
public ComboBoxItem SetPointWidth   
{ 
  get
    {
      _setWidth.Content = Chart.Width;
      _setWidth.IsSelected = true;
       return _setWidth;
    }
        set
        {
            if ((value == null) || (_setPointWidth == value))
                return;

            _setPointWidth = value;
         }
}               

【问题讨论】:

  • 我不确定您要做什么,但只需删除 SelectedItem="{Binding SetWidth}" 并为组合框使用 x:Name 绑定就足够了?
  • errmmmhh...这不是 MVVM。

标签: c# wpf mvvm combobox


【解决方案1】:

如果我的理解正确,以下将解决您的问题。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox VerticalAlignment="Center" Text="3"  SelectedItem="{Binding SetWidth}" MinHeight="20">
            <sys:String >1</sys:String>
            <sys:String >2</sys:String>
            <sys:String >3</sys:String>
            <sys:String >4</sys:String>

            </ComboBox>

    </Grid>
</Window>


 public partial class MainWindow : Window 
    {
        private string _setWidth;
        public string SetWidth
        {
            get
            {
                return _setWidth;
            }
            set
            {
                if ((value == null) || (_setWidth == value))
                    return;

                _setWidth = value;
                RaisePropertyChanged("SetWidth");
            }
        }       
        public MainWindow()
        {


            InitializeComponent();
            SetWidth = "3";
            this.DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

如果有帮助,请告诉我。 谢谢, 库马尔

【讨论】:

  • 刚刚注意到,您正在使用 SetWidth 属性进行绑定,这在 ViewModel 中不存在。将其更改为 SetPointWidth
  • 欢迎......如果您的问题得到解决,请将其标记为答案
【解决方案2】:

SelectedItem 是通过您的绑定设置的。确保您的视图模型上的 SetWidth 默认设置为 3,应该没问题

【讨论】:

  • 私有 ComboBoxItem _setWidth = new ComboBoxItem();公共 ComboBoxItem SetPointWidth { 获取 { _setWidth.Content = Chart.Width; _setWidth.IsSelected = true;返回_setWidth; }
  • 我设置了值,但在我选择项目之前它无法显示。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多