【问题标题】:How to display only the value part of my dictionary in a dynamic combobox for XAML C#?如何在 XAML C# 的动态组合框中仅显示我的字典的值部分?
【发布时间】:2015-06-24 06:15:49
【问题描述】:

大家好,我有一个 C# 中的问题模型,我用它来填充 XAML 中的列表视图。

C#问题模型

 class QuestionModel
{
    public QuestionModel(int Key, string Question, Dictionary<string, string> Answers)
    {
    key = Key;
    question = Question;
        answers=Answers;
    }

    public int key { get; set; }
    public string question { get; set; }
    public Dictionary<string, string> answers { get; set; }
}

XAML 列表视图

 <ListView x:Name="QuestionListView" Grid.Row="1"
      ItemsSource="{Binding Source={StaticResource QuestionViewSource}}" SelectionMode="None">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="1"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="500"/>
                        <ColumnDefinition Width="300"/>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="300"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock MaxWidth="500" TextTrimming="WordEllipsis" x:Name="QuestionTextBlock" VerticalAlignment="Center" FontSize="30" FontFamily="Segoe UI" FontWeight="Light" Margin="10">
                    <Run Text="{Binding question}"/>
                    </TextBlock>
                    <ComboBox Height="40" VerticalAlignment="Stretch" x:Name="QuestioncomboBox" Grid.Column="1" FontSize="25" ItemsSource="{Binding answers}">
                    </ComboBox>
                    <Button x:Name="QuestionComment" Content="Comment" Foreground="Black" Background="LightGray" FontSize="16" HorizontalAlignment="Stretch" Grid.Column="3" Height="44" FontFamily="Segoe UI" FontWeight="Light" BorderBrush="Black" BorderThickness="1"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我的问题是在我填写组合框的答案时出现的。

我使用字典将我的答案存储在模型中。所以它们的键代表答案的代码或标识符,值代表答案。

但是,当我运行应用程序时,我的组合框会同时显示答案的关键和价值。

有没有办法让我的组合框显示值而不是键和值?

【问题讨论】:

    标签: c# wpf xaml dictionary combobox


    【解决方案1】:

    您可以直接绑定到字典的值

       <ComboBox Height="40" VerticalAlignment="Stretch" x:Name="QuestioncomboBox" Grid.Column="1" FontSize="25" ItemsSource="{Binding answers.Values}">
                        </ComboBox>
    

    【讨论】:

      【解决方案2】:

      ComboBox.Displaymember 和/或 Displaymemberpath 应该可以解决这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 2018-08-26
        • 2016-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多