WPF多列组合框-绑定值是整个POCO,而不是单个POCO属性
我在WPF中构建了一个多列组合框,如下所示: (请原谅代码样本的数量;) 组合框XAMLWPF多列组合框-绑定值是整个POCO,而不是单个POCO属性,wpf,xaml,mvvm,Wpf,Xaml,Mvvm,我在WPF中构建了一个多列组合框,如下所示: (请原谅代码样本的数量;) 组合框XAML <ComboBox Name="cmbProductTypeMulti" IsEditable="False" Margin="0,2,10,2" MaxDropDownHeight="250" Text="{Binding Path=AcctData.ProductType}" I
<ComboBox Name="cmbProductTypeMulti" IsEditable="False" Margin="0,2,10,2" MaxDropDownHeight="250"
Text="{Binding Path=AcctData.ProductType}" ItemsSource="{Binding Path=ProductTypeSelection}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding Path=ProductType}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource ComboBoxItemStyle}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Name="ComboBoxItemBorder" BorderThickness="1">
<Grid Name="ComboBoxItemGrid" TextElement.Foreground="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5,3" Grid.Column="0" Text="{Binding ProductType}"/>
<TextBlock Margin="5,3" Grid.Column="1" Text="{Binding Description}" FontSize="10"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--- snip --->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
视图的Poco代码段
Public Class Account
Inherits ObservableObject
Private _ProductType As String
Public Property ProductType As String
Get
Return _ProductType
End Get
Set(value As String)
MyBase.Set(Of String)(Function() ProductType, _ProductType, value)
End Set
End Property
...
End Class
组合框的Poco
Public Class ProductType
Inherits ObservableObject
Private _ProductType As String
Private _Description As String
Public Property ProductType As String
...
End Property
Public Property Description As String
...
End Property
...
End Class
问题 首先,当我将帐户加载到视图模型的AcctData.ProductType中时,该值不会显示在multicolumn组合框中。我有一个常规的组合框绑定到同一个值,它正确地显示AcctData.ProductType初始值 其次,当我从多列组合框中选择一个项目时,常规组合框将丢失其所选项目并变为空白。当我进入debug并查看vm的AcctData.ProductType时,我发现它已经被分配了ProductType poco的ToString值 因此,看起来多列组合框正试图使用整个Poco进行绑定。如何使用Poco中的属性作为其绑定值来获取它
感谢如果您从viewmodel的构造函数调用ProductTypeSelection setter来初始化数据,第一个问题可能会得到解决,因为当前它没有被设置,因此不会引发propertychanged事件,当前您的绑定只知道默认为null的初始数据
第二个问题可能是因为xaml是在您定义的controltemplate之上使用datatemplate的,如果您将controltemplate中的xaml放在datatemplate中的话,这个问题可能会得到解决
因为多列组合框是使用Poco的ToString()值绑定的,所以我只是修改了ToString()以返回ProductType属性,这是我想要绑定的属性!现在,多列组合框的行为应该是正常的,即使它在技术上绑定到了错误的属性。您有任何绑定错误吗?通常这些看起来像“BindingExpression路径错误:…”。不,没有看到任何绑定错误。谢谢您的建议。不幸的是,我还不是什么XAML专家,我的变通方法似乎可以胜任这项工作。
Public Class ProductType
Inherits ObservableObject
Private _ProductType As String
Private _Description As String
Public Property ProductType As String
...
End Property
Public Property Description As String
...
End Property
...
End Class