![](/img/trans.png)
[英]WPF : How to do data validation in combobox selectedvalue by dependecy property
[英]How to binding a ComboBox SelectedValue property to TextBox Text property inside ListView WPF
我在ListView中有一个ComboBox和TextBox,如何将SelectedValue绑定到Text,我的代码:
<GridViewColumn Width="130">
<GridViewColumnHeader Content="Caracteristica" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox x:Name="cmbCaracteristica" Width="100" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumnHeader Content="Tipo" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox x:Name="txtTipoValor" Text="{Binding Path=SelectedValue, ElementName=cmbCaracteristica, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Left"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这行不通。 PD:C#中ComboBox的ItemSource加载。
您尝试过DisplayMemberPath属性吗? 在相同情况下为我工作。
<ListView DisplayMemberPath="MainList" </ListView>
适用于MainList中的选定项目,您可以对文本框执行相同的操作。
将ComboBox
的SelectedItem
属性绑定到IEnumerable<T>
ItemsSource
类型T
的属性,然后将TextBox
绑定到相同的source属性:
<GridViewColumn Width="130">
<GridViewColumnHeader Content="Caracteristica" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox x:Name="cmbCaracteristica" Width="100"
SelectedItem="{Binding SelectedItem}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumnHeader Content="Tipo" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox x:Name="txtTipoValor"
Text="{Binding Path=SelectedItem, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Left"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
确保类型T
实现INotifyPropertyChanged接口。
你不能在一个元素绑定CellTemplate
在另一个元素CellTemplate
使用ElementName
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.