Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何仅在combobox打开WPF时显示复杂项说明_C#_Wpf_Combobox - Fatal编程技术网

C# 如何仅在combobox打开WPF时显示复杂项说明

C# 如何仅在combobox打开WPF时显示复杂项说明,c#,wpf,combobox,C#,Wpf,Combobox,我在寻找解决问题的方法。也许你们中的一个可以给我一些建议 结构 我有一个组合框,代码如下: <GridViewColumn Header="{lex:LocText CurrentWordCode}" controls:CustomColumnInfo.Name="CurrentWordCode"> <GridViewColumn.CellTemplate> <DataTemplate>

我在寻找解决问题的方法。也许你们中的一个可以给我一些建议

结构

我有一个组合框,代码如下:

        <GridViewColumn Header="{lex:LocText CurrentWordCode}" controls:CustomColumnInfo.Name="CurrentWordCode">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="ToolsComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0"
                               SelectedItem="{Binding SelectedCurrentWord}" ItemsSource="{Binding AvailbleWords}" DisplayMemberPath="Item1"  />

                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>

        <GridViewColumn Header="{lex:LocText CurrentWordName}" DisplayMemberBinding="{Binding SelectedCurrentWord.Item2}" 
                        controls:CustomColumnInfo.Name="CurrentWordName"/>

然后选择CurrentWord

    private Tuple<string,string, string> _SelectedCurrentWord;
    public Tuple<string,string, string> SelectedCurrentWord
    {
        get { return _SelectedCurrentWord; }
        set { _SelectedCurrentWord = value;
            OnPropertyChanged("SelectedCurrentWord");
        }
    }
private Tuple\u SelectedCurrentWord;
公共元组SelectedCurrentWord
{
获取{return\u SelectedCurrentWord;}
设置{u SelectedCurrentWord=value;
OnPropertyChanged(“SelectedCurrentWord”);
}
}
我正在创建上面这样的对象:

SelectedCurrentWord = new Tuple<string, string, string>(CurrentWordCode, CurrentWordName, CurrentWordCode + " - " + CurrentWordName)
SelectedCurrentWord=新元组(CurrentWordCode、CurrentWordName、CurrentWordCode+“-”+CurrentWordName)
ItemSource如下所示:

AvailbleWords = new List<Tuple<string, string, string>>();
availablewords=newlist();
现在:

现在,图像中的第一列包含代码(元组中的第一个字符串),第二列包含名称(元组中的第二个字符串)。我的问题是,从combobox中选择项的用户不知道要选择什么项,因为只看到了代码,而看不到项的名称

目的

我的目的是在CurrentWordCode列中,用户看到代码,在CurrentWordName列中,用户看到此项的名称。现在已经完成了。但当用户点击combox时,我想用如下格式向他显示项目:“Code”+“-“+”Name”,而不是像现在这样只显示代码

出于这个原因,我创建了元组结构中的第三个项,其中有“Code”+“-”+“Name”,但问题是,如果选择了combobox中的项并打开了combobox,则combobox中显示的selected项也采用此格式,但必须只有一个“Code”


抱歉,描述复杂,如果不清楚我要找什么,请提问,我会尽量描述得更好。谢谢你的帮助。问候你,祝你度过愉快的一天

看起来您需要设置ComboBox的以下属性:

<ComboBox x:Name="ToolsComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" SelectedItem="{Binding SelectedCurrentWord}" ItemsSource="{Binding AvailbleWords}" SelectedValuePath="Item1" SelectionBoxItemTemplate="{Path_to your template}"  />

首先,您可以松开元组中的第三项,这纯粹是为了显示,不应该在那里,因为我们已经在其他两项中获得了所需的所有信息

其次,您需要使用controltemplate来告诉您的组合框如何显示其内容。实际上,你需要两个。一个用于所选项目,另一个用于下拉列表。然后,您可以使用datatemplate来确保combobox处理这两种情况

<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">
    <Window.Resources>
            <ControlTemplate x:Key="SimpleTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding Item1}" />
                </StackPanel>
            </ControlTemplate>
            <ControlTemplate x:Key="ExtendedTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Item1}" />
                    <TextBlock Text="-" />
                    <TextBlock Text="{Binding Item2}" />
                </StackPanel>
            </ControlTemplate>
            <DataTemplate x:Key="TupleTemplate">
                <Control x:Name="theControl" Focusable="False" Template="{StaticResource ExtendedTemplate}" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ComboBoxItem}}, Path=IsSelected}" Value="{x:Null}">
                        <Setter TargetName="theControl" Property="Template" Value="{StaticResource SimpleTemplate}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
    </Window.Resources>

<StackPanel>
    <ComboBox x:Name="ToolsComboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0" Width="200"
                           SelectedItem="{Binding SelectedCurrentWord}" ItemsSource="{Binding AvailbleWords}" ItemTemplate="{StaticResource TupleTemplate}" />
</StackPanel>


看起来像是的复制品。可以很容易地解决与模板选择器。此外,你还可以美化下拉列表项目。谢谢你,比约恩。代码运行良好。对你来说真的都是好事!