Question

I am working on a WPF and have hit a serious wall. I have a data set that has two columns, ContactName and ContactTitle. I have successfully loaded all of the data into a ComboBox and even sorted it by ContactName. However, I am trying to now access that data and display part of it in a TextBox. (This is of course just a proof of concept type exercise, the final product will populate a variety of TextBoxes with the selected persons information). The problem is, I cannot get the info to populate in the TextBox. Here is the code that I have:

    using System.Windows;
    using System.Windows.Controls;
    using System.ComponentModel;

    namespace MultiBindingInWPF_CS
    {

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            //Create DataSet
            CustomersDataSet customerDataSet = new CustomersDataSet();

            //Create DataTableAdapter
            CustomersDataSetTableAdapters.CustomersTableAdapter taCustomers = new CustomersDataSetTableAdapters.CustomersTableAdapter();
            taCustomers.Fill(customerDataSet.Customers);
            //Sort Data
            SortDescription sd = new SortDescription("ContactName", ListSortDirection.Descending);
            //Designate ItemSource
            this.ComboBox1.ItemsSource = customerDataSet.Customers;
            //Apply Sort
            this.ComboBox1.Items.SortDescriptions.Add(sd);
        }

        private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //Using SelectedIndex only to prove connection to TextBox is working
                textBox1.Text = ComboBox1.SelectedIndex.ToString();
            }
            catch
            {
                textBox1.Text = "Invalid";
            }
        }
    }
}

Then here is my XAML:

<Window x:Class="MultiBindingInWPF_CS.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="Multibinding in WPF" Height="163" Width="300">

    <Grid Loaded="Grid_Loaded">
        <StackPanel Name="StackPanel1" Margin="12">
            <Label Height="28" Name="Label1">List of Customers (Name AND Title :-) )</Label>
            <ComboBox Height="23" Name="ComboBox1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" IsTextSearchEnabled="True" SelectionChanged="ComboBox1_SelectionChanged" SelectedValue="{Binding Path=CustomerID}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock DataContext="{Binding}">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0} - {1}">
                                    <Binding Path="ContactName" />
                                    <Binding Path="ContactTitle" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBox Height="23" Name="textBox1" Width="120"/>
        </StackPanel>
    </Grid>
</Window>

My ultimate goal would be to populate the TextBox dynamically by getting the selected value, and getting the info in the dataset associated with that CustomerID, but just getting the SelectedItem's text to populate in the TextBox would be a huge step.

Any help is GREATLY appreciated. Thanks all.

Was it helpful?

Solution

Give this a try; it removes the changed event handler and leverages binding.

<Grid Loaded="Grid_Loaded">
    <StackPanel Name="StackPanel1" Margin="12">
        <Label Height="28" Name="Label1">List of Customers (Name AND Title :-) )</Label>
        <ComboBox Height="23" Name="ComboBox1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" IsTextSearchEnabled="True" SelectedValue="{Binding Path=CustomerID}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} - {1}">
                                <Binding Path="ContactName" />
                                <Binding Path="ContactTitle" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <TextBox Height="23" Name="textBox1" Text="{Binding ElementName=ComboBox1, Path=SelectedItem.ContactName}" Width="120"/>
    </StackPanel>
</Grid>

Check out this SO answer as well, which details the differences between SelectedItem, SelectedValue, and SelectedValuePath and is ultimately the issue most people run into.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top