XAML 绑定:组合框在一种情况下填充文本框,但不是另一种情况? WPF

XAML Binding: Combobox populating textbox in one scenario, but not another? WPF

我有一组代码可以正常工作。它从包含 [ID、Fname、Lname、地址、zip 等] 的用户记录数据库中加载 "address" 字段到 ComboBox 的 selection 集中。一旦用户select输入地址,它也会在相应的文本框中显示selection。

工作代码:

<Window x:Class="CC_Ticketing_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CC_Ticketing_WPF"
    mc:Ignorable="d"
    Title="MainWindow" Height="800" Width="600">
    <Window.Resources>
        <DataTemplate x:Key="orderheaderTemplate">
            <StackPanel>
                <TextBlock Text="{Binding XPath=address}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <!-- I think this what you said populated properly in the comments -->
        <ComboBox x:Name="cb_Address" DataContext="{StaticResource orderheaderDataSource}" ItemsSource="{Binding XPath=/dataroot/orderheader}" DisplayMemberPath="address" HorizontalAlignment="Left" VerticalAlignment="Top" Width="90" IsEditable="True"/>
        <!-- this should replicate what you see in the combobox -->
        <TextBlock x:Name="tb_Address" Text="{Binding ElementName=cb_Address,Path=Text}"/>
    </StackPanel>
</Window>

问题是我知道这非常有限,因为它依赖于在堆栈面板中定义所有内容。我正在尝试沿着这些方向做这件事:

断码

<Window x:Class="CC_Ticketing_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CC_Ticketing_WPF"
    mc:Ignorable="d"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="MainWindow" Height="800" Width="600">
    <Window.Resources>
        <DataTemplate x:Key="orderheaderTemplate">
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ComboBox x:Name="cb_Address" DataContext="{StaticResource orderheaderDataSource}" ItemsSource="{Binding XPath=/dataroot/orderheader}" DisplayMemberPath="address" SelectedValuePath="Content" HorizontalAlignment="Left" VerticalAlignment="Top" Width="90" IsEditable="True"/>
        <TextBlock x:Name="tb_Address" Text="{Binding ElementName=cb_Address,Path=SelectedValue,Mode=OneWay}"/>
    </StackPanel>
</Window>

这会加载地址并允许 selection 在组合框中,但不会向文本框发送任何内容。最后,我希望 select 从这个组合框中输入一个地址,不仅可以将 selection 发送到文本框,还可以将该记录中的其他相关数据发送到其他文本框(比如你 select 只是一个地址,它会用地址、名称、邮编等填充一堆文本框。)很常见的业务需求。有人可以让我走上正确的轨道来完成这个吗?

将文本框上的路径更改为 SelectedValue 而不是 SelectedItem.Content

这是我做的,而且很管用。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <StackPanel>
        <ComboBox x:Name="cb_Address" DisplayMemberPath="address" SelectedValuePath="content" HorizontalAlignment="Left" VerticalAlignment="Top" Width="90" IsEditable="True"/>
        <TextBlock x:Name="tb_Address" Text="{Binding SelectedValue, ElementName=cb_Address, Mode=OneWay}"/>
    </StackPanel>

</Grid>

隐藏代码进行测试

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollection<OrderHeader> Data = new ObservableCollection<OrderHeader>();

    public MainWindow()
    {
        InitializeComponent();


        for (int i = 0; i <= 100; i++)
        {
            Data.Add(new OrderHeader { address = "Address_" + i.ToString(), content = "Content_"+i.ToString() });
        }

        cb_Address.ItemsSource = Data;
    }

    public class OrderHeader
    {
        public string address {  get; set;}
        public string content { get; set; }
    }
}

您可以,只需将下面的代码与正确的组合框绑定即可。

<TextBlock x:Name="tb_Address">
            <Run Text="{Binding SelectedValue, ElementName=cb_Firstnames, Mode=OneWay}" />
            <Run Text=" " />
            <Run Text="{Binding SelectedValue, ElementName=cb_Lastnames, Mode=OneWay}" />
        </TextBlock>