将组合框显示文本设置为ObservableCollection< T>的属性。

编程入门 行业动态 更新时间:2024-10-19 19:40:56
本文介绍了将组合框显示文本设置为ObservableCollection< T>的属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下要绑定到组合框的集合:

I have the following collection which i would like to bind to a combobox:

public ObservableCollection<Parameter> Values { get; set; } public class Parameter { public String Text { get; set; } public String Value { get; set; } }

我需要将组合框的显示文本绑定到的Text属性。 Parameter类,下面我尝试了以下方法,但都无济于事:

I need to bind the display text of the combobox to the Text property of the Parameter class, I've tried the following ways below but all to no avail:

<ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Parameter.Text" <ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Parameter\Text" <ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text"

当我尝试[程序集名称]上方的3种方法时。

When I try the 3 methods above the [assembly name].Parameter is displayed in the combobox for each parameter.

以上两种方法在组合框中均不显示任何内容

The 2 methods above don't display anything in the combobox

<ComboBox ItemsSource="{Binding Values, Path=Text}" <ComboBox ItemsSource="{Binding Values, Path=Values.Text}"

然后,该函数将第一个参数的文本拆分为字符,并将每个字符显示为单独它em在组合框中:

And this one takes the text of the first parameter,splits it into characters and displays each character as a seperate item in the combobox:

<ComboBox ItemsSource="{Binding Values, Path=Values/Text}"

更新:

这是完整的XAML代码

This is the complete XAML code as requested

<ListBox BorderBrush="{x:Null}" Grid.Column="0" Height="100" Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding ItemParams}" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="2" HorizontalContentAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock TextTrimming="CharacterEllipsis" Grid.Column="0" Margin="2" Text="{Binding Name}" Background="{Binding ElementName=cmbColors, Path=SelectedItem}"/> <ComboBox ItemsSource="{Binding Values}" DisplayMemberPath="Text" SelectedIndex="0" HorizontalAlignment="Left" Grid.Column="1" Margin="2" Width="150" Name="cmbColors" > <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

Params类:

public class Params { public Params(String name, ObservableCollection<Parameter> values) { Name = name; Values = values; } public String Name { get; set; } public ObservableCollection<Parameter> Values { get; set; } }

推荐答案

只需删除组合框项目模板或设置 Text = {Binding Text}

Had to simply remove the combobox item template or set Text="{Binding Text}"

<ComboBox.ItemTemplate> <DataTemplate> <TextBlock Grid.Column="1" Margin="2, 1" Text="{Binding}"/> </DataTemplate> </ComboBox.ItemTemplate>

更多推荐

将组合框显示文本设置为ObservableCollection&lt; T&gt;的属性。

本文发布于:2023-11-07 15:08:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   设置为   属性   文本   ObservableCollection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!