DanBrum
DanBrum

Reputation: 439

Binding to ComboBoxItem content when Content is not just a string WPF

I have a ComboBoxwith 3 ComboBoxItems each containing an image and text in a stack panel. I want to bind the SelectedValue to the TextBlock text but can't just bind to content as this returns the stack panel. How do I bind the SelectedValue to the child TextBlock control? I don't need to notify anything else I just need the SelectedValue to return the string.

<ComboBox>
  <ComboBoxItem>
    <StackPanel Orientation="Horizontal">
        <Image Source="ImagePath/Image.bmp"/>
        <TextBlock Text="MyTextString"/>
    </StackPanel>
  </ComboBoxItem>
</ComboBox>   

Upvotes: 0

Views: 2004

Answers (2)

blindmeis
blindmeis

Reputation: 22445

an easy way would be to hold your coboboxitem information within a wrapper and put a collection of these wrapper as the itemssource of your combobox.

public class MyComboboxData
{
    public string MyImagePath { get; set; }
    public string MyText { get; set; }
}

in your codebehind:

    public ObservableCollection<MyComboboxData> MyData { get; private set; }

    public MyViewWithCombobox()
    {
        InitializeComponent();

        this.MyData = new ObservableCollection<MyComboboxData>()
                          {
                              new MyComboboxData(){MyImagePath = "ImagePath/Image.bmp", MyText = "MyTextString"},
                              new MyComboboxData(){MyImagePath = "ImagePath/Image2.bmp", MyText = "MyTextString2"}
                          };

        this.DataContext = this;
    }

now you can simply bind to all what you want:

<Grid>
    <ComboBox Name="comboBox1" ItemsSource="{Binding MyData}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImagePath}"/>
                    <TextBlock Text="{Binding MyText}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <TextBlock Text="{Binding ElementName=comboBox1, Path=SelectedItem.MyText}"/>
</Grid>

ps: take a look at MVVM with viewmodel and binding these tasks are very easy to achieve

Upvotes: 1

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

SelectedValue is actually property related to binding. in your case you are creating combobox items statically.

check this

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvaluepath.aspx

in your case you can add Tag property on ComboBoxItem

<ComboBox Height="40" x:Name="cmb" SelectedValuePath="">
            <ComboBoxItem Tag="MyTextString">
                <StackPanel Orientation="Horizontal" >
                    <Image Source="ImagePath/Image.bmp"/>
                    <TextBlock Text="MyTextString"/>
                </StackPanel>
            </ComboBoxItem>
        </ComboBox>

and access it in code

 MessageBox.Show((cmb.SelectedItem as ComboBoxItem).Tag.ToString());

Upvotes: 1

Related Questions