C# 如何将datatable绑定到wpf可编辑组合框:选择EdItem showing System.Data.DataRowView
我将datatable绑定到组合框,并在itemTemplate中定义了dataTemplate。我可以在组合框下拉列表中看到所需的值,在selectedItem中看到的是C# 如何将datatable绑定到wpf可编辑组合框:选择EdItem showing System.Data.DataRowView,c#,wpf,data-binding,wpf-controls,binding,C#,Wpf,Data Binding,Wpf Controls,Binding,我将datatable绑定到组合框,并在itemTemplate中定义了dataTemplate。我可以在组合框下拉列表中看到所需的值,在selectedItem中看到的是System.Data.DataRowView以下是我的代码: <ComboBox Margin="128,139,123,0" Name="cmbEmail" Height="23" VerticalAlignment="Top" TabIndex="1" ToolTip="enter the email you s
System.Data.DataRowView
以下是我的代码:
<ComboBox Margin="128,139,123,0" Name="cmbEmail" Height="23" VerticalAlignment="Top" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=username}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
我一直在寻找类似SelectedValueTemplate或SelectedItemTemplate的东西来做同样的数据模板,但我没有找到
我想问一下我是否做错了什么,或者这是combobox绑定的已知问题?如果我的代码有问题,请给我指出正确的方向。
感谢您阅读此默认情况下,组合框将对所选项目调用ToString(),以获取要显示的值-由于您绑定到DataRowView,因此结果是类型(ToString()的默认行为) 您真正想要显示的是所选项目的username属性,为此,您可以将combobox的 (此外,如果您这样做,您可能会发现您也可以摆脱自定义数据模板,因为用户名也将用于填充每个项目。)
针对你的评论: 我不想成为那些程序员中的一员,但“它在我的机器上工作” 我的XMAL是:
<ComboBox Name="cmbEmail"
Height="23"
VerticalAlignment="Top"
TabIndex="1"
ToolTip="enter the email you signed up with here"
IsEditable="True"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
DisplayMemberPath="username">
</ComboBox>
默认情况下,组合框将对所选项目调用ToString(),以获取要显示的值-由于您绑定到DataRowView,因此结果为类型(ToString()的默认行为) 您真正想要显示的是所选项目的username属性,为此,您可以将combobox的 (此外,如果您这样做,您可能会发现您也可以摆脱自定义数据模板,因为用户名也将用于填充每个项目。)
针对你的评论: 我不想成为那些程序员中的一员,但“它在我的机器上工作” 我的XMAL是:
<ComboBox Name="cmbEmail"
Height="23"
VerticalAlignment="Top"
TabIndex="1"
ToolTip="enter the email you signed up with here"
IsEditable="True"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
DisplayMemberPath="username">
</ComboBox>
我已经找到了它不起作用的原因。使用DisplayMemberPath不起作用,而是使用ItemTemplate。 这里有一个例子
<Window.Resources>
<DataTemplate x:Key="comboTemplate">
<TextBlock Text="{Binding Path=username}" />
</DataTemplate>
</Window.Resources>
<ComboBox Margin="18,121,24,0" Name="cmbEmail" Tag="email" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource comboTemplate}" ItemsSource="{Binding}" Height="23" VerticalAlignment="Top" Style="{DynamicResource cmbBoxerrors}">
<ComboBox.Text>
<Binding Path="username"/>
</ComboBox.Text>
</ComboBox>
请注意ItemTemplate
xaml.cs代码没有更改。感谢那些阅读并向我推荐解决方案的人。我已经找到了它不起作用的原因。使用DisplayMemberPath不起作用,而是使用ItemTemplate。 这里有一个例子
<Window.Resources>
<DataTemplate x:Key="comboTemplate">
<TextBlock Text="{Binding Path=username}" />
</DataTemplate>
</Window.Resources>
<ComboBox Margin="18,121,24,0" Name="cmbEmail" Tag="email" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource comboTemplate}" ItemsSource="{Binding}" Height="23" VerticalAlignment="Top" Style="{DynamicResource cmbBoxerrors}">
<ComboBox.Text>
<Binding Path="username"/>
</ComboBox.Text>
</ComboBox>
请注意ItemTemplate
xaml.cs代码没有更改。感谢那些阅读并向我推荐解决方案的人。我从msdn中得到了答案,它为我做到了
<ComboBox IsEditable="True" ItemTemplate="{StaticResource comboTemplate}" ItemsSource="{Binding}" TextSearch.TextPath="username">
我从msdn得到了答案,它帮了我
<ComboBox IsEditable="True" ItemTemplate="{StaticResource comboTemplate}" ItemsSource="{Binding}" TextSearch.TextPath="username">
谢谢你的提示。但是有一个问题。我做了displaymemberPath位并对datatemplate部分进行了注释。我得到的结果是相反的情况,selectedItem正确显示,dropdownlist显示System.Data.DataRowView。我试图在使用DisplayMemberPath和ItemTemplate时保留这两个选项,但我发现一个异常说我不能同时使用它们。再次感谢您的帮助我的环境一定出了问题,因为您的代码的复制粘贴与selectedItem中显示的内容相同,dropdownlist显示System.Data.DataRowView。其他人能试试吗?谢谢你的提示。但是有一个问题。我做了displaymemberPath位并对datatemplate部分进行了注释。我得到的结果是相反的情况,selectedItem正确显示,dropdownlist显示System.Data.DataRowView。我试图在使用DisplayMemberPath和ItemTemplate时保留这两个选项,但我发现一个异常说我不能同时使用它们。再次感谢您的帮助我的环境一定出了问题,因为您的代码的复制粘贴与selectedItem中显示的内容相同,dropdownlist显示System.Data.DataRowView。其他人能试试吗?谢谢,我知道它是和旧问题,但当你们合并你们的答案时,阅读它会更容易。我知道它是和旧问题,但当你们合并你们的答案时,阅读它会更容易。