在TextBox中显示ComboBox.SelectedValue

时间:2012-02-17 21:08:26

标签: wpf combobox textbox dataset multibinding

我正在研究一个WPF并且遇到了严重问题。我有一个包含两列的数据集,ContactName和ContactTitle。我已成功将所有数据加载到ComboBox中,甚至按ContactName对其进行了排序。但是,我现在尝试访问该数据并在TextBox中显示它的一部分。 (这当然只是概念类型练习的证明,最终产品将填充各种TextBoxes与所选人员信息)。问题是,我无法在TextBox中填充信息。这是我的代码:

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

然后这是我的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>

我的最终目标是通过获取所选值来动态填充TextBox,并获取与该CustomerID相关联的数据集中的信息,但只是将SelectedItem的文本填充到TextBox中将是一个巨大的进步。

非常感谢任何帮助。谢谢大家。

1 个答案:

答案 0 :(得分:3)

尝试一下;它会删除已更改的事件处理程序并利用绑定。

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

还可以查看此SO answer,其中详细说明了SelectedItemSelectedValueSelectedValuePath之间的差异,并且最终是大多数人遇到的问题。