سؤال

My Combobox:

 <pmControls:pmComboBox Grid.Row="3" Grid.Column="1" Margin="3"  
  SelectedItem="{Binding Applicable_For,Mode=Two Way}" DisplayMemberPath="Applicable_For"
     SelectedValuePath="Applicable_For">

  <pmControls:pmComboBoxItem Content="Parcel" ></pmControls:pmComboBoxItem>
  <pmControls:pmComboBoxItem Content="Property"></pmControls:pmComboBoxItem>

  </pmControls:pmComboBox>

Have Added 2 static items to combobox as parcel and property and want to get these values using binding .

I have given binding to SelectedItem and my binding field is Applicable_For.

Using above code am getting value as null in Applicable_For.

EDIT: I have added Mode=Two Way for Selected Item which I have forgot before.

But not it getting value as namespace like 'PropMgmt.Controls.pmComboBoxItem'

Please Help..

هل كانت مفيدة؟

المحلول

Instead of adding static items to combo box you can create a collection for it. for ex. Create class like:

public class KeyValuePair
{
    string key;

    public string Key
    {
        get { return key; }
        set { key = value; }
    }
    string value;

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }      

}

Then in your view model add following code:

        ObservableCollection<KeyValuePair> applicable_For_KeyValues = new ObservableCollection<KeyValuePair>();

        KeyValuePair k1 = new KeyValuePair() { Key = "1", Value = "Parcel" };
        KeyValuePair k2 = new KeyValuePair() { Key = "2", Value = "Property" };

        applicable_For_KeyValues.Add(k1);
        applicable_For_KeyValues.Add(k2);

Then in xaml add following:

<pmControls:pmComboBox Grid.Row="3" Grid.Column="1" Margin="3" 
 ItemsSource="{Binding Applicable_For_KeyValues}" 
 SelectedValue="{Binding Applicable_For,Mode=TwoWay}" SelectedValuePath="Value">
                <pmControls:pmComboBox.ItemTemplate >
                    <DataTemplate>
                        <TextBlock Text="{Binding Value}"></TextBlock>
                    </DataTemplate>
                </pmControls:pmComboBox.ItemTemplate>        

            </pmControls:pmComboBox>

Hope this solve your problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top