gpt4 book ai didi

c# - 如何在 XAML C# 的动态组合框中仅显示字典的值部分?

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:46 25 4
gpt4 key购买 nike

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

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

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

当我将答案填充到组合框时,我的问题就出现了。

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

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

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

最佳答案

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

关于c# - 如何在 XAML C# 的动态组合框中仅显示字典的值部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com