在Visual Studio、c#、Microsoft Blend、XAML中设置组合框以更新文本框

在Visual Studio、c#、Microsoft Blend、XAML中设置组合框以更新文本框,c#,wpf,xaml,combobox,visual-studio-2015,C#,Wpf,Xaml,Combobox,Visual Studio 2015,我正在使用Microsoft Blend将visual studio 2015中的一个程序从Access切换到C#。简言之,我有一个组合框标记为cb_地址。它通过以下方式绑定到XAML中名为“orderheader”的数据库中名为“address:的字段: <ComboBox x:Name="cb_Address" HorizontalAlignment="Left" Margin="10,158.71,0,0" VerticalAlignment="Top" Width=

我正在使用Microsoft Blend将visual studio 2015中的一个程序从Access切换到C#。简言之,我有一个组合框标记为cb_地址。它通过以下方式绑定到XAML中名为“orderheader”的数据库中名为“address:的字段:

        <ComboBox x:Name="cb_Address" HorizontalAlignment="Left" Margin="10,158.71,0,0" VerticalAlignment="Top" Width="90" IsEditable="True" ItemTemplate="{DynamicResource orderheaderTemplate}" ItemsSource="{Binding XPath=/dataroot/orderheader}">
到目前为止,我还没有找到一个关于如何使用这个Blend/c#环境执行事件处理程序的清晰教程

 private void cb_Address_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
但作为c#的新手,我还没有找到更新tb#U地址文本框的代码。我只是在玩

cb_AddressItem cbi = ((sender as cb_Address).SelectedItem as Lcb_AddressItem);
但我不知道如何继续下去。任何帮助都将不胜感激


编辑1:根据下面的建议,我现在将此标题设置为我的窗口XAML

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

这分别作为我的组合框和文本框:

<ComboBox x:Name="cb_Address" HorizontalAlignment="Left" Margin="10,158.71,0,0" VerticalAlignment="Top" Width="90" IsEditable="True" ItemTemplate="{DynamicResource orderheaderTemplate1}" ItemsSource="{Binding XPath=/dataroot/orderheader}" SelectedValuePath="Content">

<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="41,214,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="35.824" Width="154" FontSize="21.333" FontFamily="Adobe Arabic" Text="{Binding ElementName=cb_Address,Path=SelectedItem.Content,Mode=OneWay}">

不幸的是,从组合框中选择一条记录仍然只会填充一个数字,而文本框中什么也没有。我尝试在两个snippits中用“address”替换“Content”,以防它是直接数据库标签,但这提供了相同的结果


编辑2:

“orderheaderTemplate”的任何实例的位置

在MainWindow.xaml中,就在xmlns声明下面:

<Window.Resources>
        <DataTemplate x:Key="orderheaderTemplate">
            <StackPanel>
                <TextBlock Text="{Binding XPath=address}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="orderheaderTemplate1">
            <StackPanel>
                <TextBlock Text="{Binding XPath=address}"/>
            </StackPanel>
        </DataTemplate>
</Window.Resources>
<Application.Resources>

        <XmlDataProvider x:Key="orderheaderDataSource" Source="C:\Users\User\Desktop\C#_Ticketing\orderheader.xml" d:IsDataSource="True"/>

</Application.Resources>

在App.xaml中,就在xmlns声明下面:

<Window.Resources>
        <DataTemplate x:Key="orderheaderTemplate">
            <StackPanel>
                <TextBlock Text="{Binding XPath=address}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="orderheaderTemplate1">
            <StackPanel>
                <TextBlock Text="{Binding XPath=address}"/>
            </StackPanel>
        </DataTemplate>
</Window.Resources>
<Application.Resources>

        <XmlDataProvider x:Key="orderheaderDataSource" Source="C:\Users\User\Desktop\C#_Ticketing\orderheader.xml" d:IsDataSource="True"/>

</Application.Resources>


编辑3

概述:最简单的说法是:我在Visual Studio 2015 Blend中创建了一个xml数据源。我将该xml数据源命名为orderheaderDatasource,并将其链接到我的数据库,该数据库又称为orderheader。该xml orderheader数据库有许多记录,每个记录都有几个字段,如地址、城市、fname、lname等

我需要做的就是让一个组合框用每个记录的地址字段填充自己,然后当用户选择一个时,将该地址字段发送到一个文本框

回溯后的这一点上的整个干净的石板Xaml:

<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>
        <XmlDataProvider x:Key="orderheaderDataSource" XPath="/Info">
            <x:XData>
                <Info xmlns="">
                    <Order ID="001" Address="Example 1" />
                    <Order ID="002" Address="Example 2" />
                    <Order ID="003" Address="Example 2" />
                </Info>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <StackPanel>
        <ComboBox x:Name="cb_Address" DataContext="{StaticResource orderheaderDataSource}" ItemsSource="{Binding XPath=Order}" DisplayMemberPath="@Address" HorizontalAlignment="Left" VerticalAlignment="Top" Width="90" IsEditable="True"/>
        <TextBlock x:Name="tb_Address" Text="{Binding ElementName=cb_Address,Path=Text}"/>
    </StackPanel>

</Window>

目前所做的就是用 例1 例2 例3 选择后,它会发送到文本框

尽一切努力将组合框链接到orderheader的“地址”“字段,然后将其发送到文本框失败。我可以让combobox使用一些早期代码正确地填充orderheader中的地址,但仅此而已。在选择后,没有任何东西会将地址发送到文本框,经过一周的尝试和Cadogi的良好帮助,在XAML第一次表现出明显的仇恨本性之后,我陷入了疯狂和怀疑的真空


最终编辑|工作代码

<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>
        <ComboBox x:Name="cb_Address" DataContext="{StaticResource orderheaderDataSource}" ItemsSource="{Binding XPath=/dataroot/orderheader/address}"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="90" IsEditable="True"/>
        <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"
    Title="MainWindow" Height="800" Width="600">
<StackPanel>
<!-- I think this what you said populated properly in the comments -->
    <ComboBox x:Name="cb_Address" ItemsSource="{Binding XPath=/dataroot/orderheader/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>

我会把它保存在XAML中,以保持它的整洁-

在组合框中添加SelectedValuePath属性:

<ComboBox x:Name="cb_Address" HorizontalAlignment="Left" Margin="10,158.71,0,0" VerticalAlignment="Top" Width="90" IsEditable="True" ItemTemplate="{DynamicResource orderheaderTemplate}" ItemsSource="{Binding XPath=/dataroot/orderheader}" SelectedValuePath="Content" />
这使得DataContext和DisplayMemberPath在直接指向地址时变得冗余

使用上述代码

<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>
        <ComboBox x:Name="cb_Address" DataContext="{StaticResource orderheaderDataSource}" ItemsSource="{Binding XPath=/dataroot/orderheader/address}"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="90" IsEditable="True"/>
        <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"
    Title="MainWindow" Height="800" Width="600">
<StackPanel>
<!-- I think this what you said populated properly in the comments -->
    <ComboBox x:Name="cb_Address" ItemsSource="{Binding XPath=/dataroot/orderheader/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>

这应该基于您给出的反馈而起作用。这是非常基本的,并且如果您想使用模板或任何多绑定,它将限制您显示数据的方式。希望它能按原样为你工作

您自己所做的就是在组合框中的ItemsSource属性中使用“/address”跳过我上一篇文章中的DisplayMemberPath。 然后文本将在TextBlock中绑定到ComboBox的text属性


或者,您可以在您的原始帖子中解决:

XAML:(在您的组合框中)

C#:


假设纯XAML方法继续失败。

tb_Address.Value=cb_Address.Text或cb_Address.Value如何?您的所有数据都在一个已经分解为业务模型的数据库中?!这是应用MVVM并正确设计应用程序的完美情况。也就是说,您不需要使用代码隐藏。只是一个think.Address.Value和.Text抛出异常,就好像cb_地址和tb_地址在xaml中不存在一样。我很想在MVVM中这样做,但我不知道从哪里开始,现在查看文档。感谢您的回复,我觉得这是我需要走的路线。我在问题的底部贴了一个编辑条,上面有我替换的内容,但还没有改变结果。如果有助于了解该过程,那么在blend中,我从“orderheader”db创建了一个xml数据源,并将地址:(字符串)标签拖到cb_地址组合框上以创建初始代码。这允许我搜索地址记录,但一旦我选择了地址,框本身就会填充一个增量参考号。我还没有尝试过将任何信息发送到tb_地址。您的编辑确实有效。我回到了你最初的建议,我正在寻找装订中的错误。下面是一幅图:要获取数据,我只需点击绿色+以创建XML数据源,命名为orderheaderTemplate,您可以看到orderheader数据库及其导入的字段。我应该在哪里检查orderheaderTemplate、orderheader本身和地址之间的关系?似乎已经全部设置好了..在组合框中有如下属性:ItemTemplate=“{DynamicResource orderheaderTemplate}”。您需要在MainWindow.xaml、app.xaml或词典(如果有的话)中找到该样式(orderheaderTemplate)。然后检查您的数据绑定,上面应该是ResourceDictionary-我没有时间在上面的编辑2中编辑,我已经放置了我能找到的orderheaderTemplate的每个实例。我为额外的损失道歉
<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">
<StackPanel>
<!-- I think this what you said populated properly in the comments -->
    <ComboBox x:Name="cb_Address" ItemsSource="{Binding XPath=/dataroot/orderheader/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>
SelectionChanged="cb_Address_SelectionChanged"
private void cb_Address_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
tb_Address.Text = ((ComboBoxItem)cb_Address.SelectedItem).Content.ToString;
}