简体   繁体   中英

How to display only the value part of my dictionary in a dynamic combobox for XAML C#?

Hi guys I have a question model in C# which I use to populate a listview in XAML.

C# question model

 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>

My issue occurs when I populate my answers to the combo box.

I use a dictionary to store my answers in the model. So they key represents the code or identifier for the answer and the value represents the answer.

However when I run the app my combo box shows me both the key and value of my answer.

Is there a way I could just get my combo box to show the value rather than both key and value?

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

You can bind directly to Values of Dicionary

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM