【发布时间】:2015-08-31 20:45:54
【问题描述】:
我有一组代码可以正常工作。它将“地址”字段从包含诸如 [ID、Fname、Lname、地址、zip 等] 的用户记录数据库加载到 ComboBox 的选择集中。一旦用户选择了一个地址,它也会在相应的文本框中显示该选择。
工作代码:
<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>
这会加载地址并允许在组合框中进行选择,但不会向文本框发送任何内容。最终,我希望从此组合框中选择一个地址,不仅将选择发送到文本框,而且还将该记录中的其他相关数据发送到其他文本框(就像您只选择一个地址并填充一堆文本框一样地址、姓名、邮编等)非常常见的业务需求。有人可以让我在正确的轨道上完成此任务吗?
【问题讨论】:
标签: wpf database xaml binding combobox