简体   繁体   中英

Default value for Combo box in WPF MVVM

i am using the below code for displaying num in ComboBox,

but the default text 3 is not displaying.Please Kindly help.....

Please Kindly help.....

Waiting for your answers.

<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>    

Behind code:

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 is set through your binding. Make sure SetWidth on your view model is set to 3 by default, and it should be OK

If My understanding is correct below will solve your problem.

<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));
        }
    }

let me know if it helps. Thanks, Kumar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM