WPF: binding data to ComboBox to int value

I have a class like this:

public class Service
{
    public int State { get; set; }
}

How can I bind it to a ComboBox, so “Active” / “Inactive” is displayed in the combo, and the state becomes 1 or 0?

<ComboBox SelectedItem="{Binding Path=State}">
    <ComboBoxItem Content="Active" Tag="1" IsSelected="True"/>
    <ComboBoxItem Content="Inactive" Tag="0" />
</ComboBox>

I bind an object to a form using a DataContext form.DataContext = new Service();

+4
source share
4 answers

usually you use a value converter , but in this case you can easily use a dictionary.

Something like this should work:

public class Service
{
   public Dictionary<int,string> StateDictonary {get;set;}

    public Service()
    {
      StateDictonary = new Dictionary<int,string>();
      StateDictonary[1] = "Active";
      StateDictonary[0] = "Inactive";
    }
}

Then in xaml you will do something like:

<ComboBox ItemsSource="{Binding StateDictonary }"   DisplayMemberPath="Value" SelectedValuePath="Key" />

I'm at home now, so I can’t check it. so let me know if that doesn't work

+1
source

, Tag , , SelectedItem. ComboBoxItems datacontext, , :

<ComboBox SelectedValue="{Binding Path=State}" SelectedValuePath="Tag">
    <ComboBoxItem Content="Active" Tag="1" IsSelected="True"/>
    <ComboBoxItem Content="Inactive" Tag="0" />
</ComboBox>
+2

:

  • , SelectedIndex SelectedItem. , , , .

  • "State" int . ItemsSource , "State" . DisplayMemberPath string. int SelectedItem. , , . int ( ).

  • "State" ( ) , . , , "real" int .

, , , enum int. , .

0

ItemsSource ComboBox. ComboBox, ComboBoxItem ItemsSource SelectedItem, . , int, combobox ItemsSource, .

, -

public class Service
{
    private State _selectedState;
    private readonly ObservableCollection<State> _states = new ObservableCollection<State>() { new State() { Value = "Active", IsSelected = true} ,new State() {Value = "Inactive" }};

    public State SelectedSelectedState
    {
        get { return _selectedState; }
        set
        {
            _selectedState = value;
            BitState = _selectedState.Value == "Active" ? 1 : 0;
        }
    }

    public int BitState { get; set; }

    public class State
    {
        public bool IsSelected { get; set; }
        public string Value { get; set; }
    }

    public ObservableCollection<State> States
    {
        get { return _states; }
    }
}

MainWindow

public MainWindow()
{
 InitializeComponent();
 comboBox.DataContext = new Service();
}

, - ,

    public enum State
    {
        Inactive,
        Active,
    }

    public class Service
    {
        private readonly ObservableCollection<State> _states = new ObservableCollection<State>() { State.Inactive, State.Active };
        private int _selectedValue;

        public int SelectedValue
        {
            get { return _selectedValue; }
            set { _selectedValue = value; }
        }

        public ObservableCollection<State> States
        {
            get { return _states; }
        }
    }

XAML

    <ComboBox x:Name="blah" SelectedIndex="{Binding Path=SelectedValue, Mode=TwoWay}" ItemsSource="{Binding States}"/>
0

Source: https://habr.com/ru/post/1531399/


All Articles