我有一组代码正常工作。它将“地址”字段从包含[ID,Fname,Lname,address,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>
这会加载地址并允许在组合框中进行选择,但不会向文本框发送任何内容。最后我想要从这个combox中选择一个地址,不仅要将选择发送到文本框,还要将该记录中的其他相关数据发送到其他文本框(就像你只选择一个地址一样,它会填充一堆文本框地址,名称,邮编等。)非常常见的业务需求。有人能把我放在正确的轨道上来实现这个目标吗?
答案 0 :(得分:1)
将文本框上的路径更改为SelectedValue而不是SelectedItem.Content
答案 1 :(得分:1)
这就是我所做的并且有效。
<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; }
}
}
答案 2 :(得分:1)
您只需使用正确的组合框绑定下面的代码即可。
<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>