嗨,我在 ComboBox 中绑定时遇到问题。我想将 ComboBox 项绑定到 ListView 列,并作为选定列上定义的附加属性的选定值返回值。
在下面的示例中,您可以看到显示所选列宽度的工作示例。如果您尝试将ComboBox中的SelectedValuePath更改为(loc:SampleBehavior.SampleValue),则会出现绑定错误:
BindingExpression 路径错误:在“对象”“GridViewColumn”上找不到“(u:SearchableListView.SearchMemberPath)”属性
<窗口 x:Class="Problem_Sample1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="clr-命名空间:Problem_Sample1" WindowStartupLocation="CenterScreen" 标题="窗口1" 高度="300" 宽度="300"> <停靠面板> <组合框 DockPanel.Dock="顶部" x:名称="组合框" ItemsSource="{绑定路径=View.Columns, ElementName=listview}" DisplayMemberPath="标题" SelectedValuePath="宽度"> </组合框> <StatusBar DockPanel.Dock="底部"> <文本块> <TextBlock Text="选中的列(值):" /> <TextBlock Text="{Binding Path=SelectedValue, ElementName=combobox}" /> </文本块> </状态栏> <ListView x:Name="listview"> <ListView.View> <网格视图> <GridViewColumn 标题="名称" 宽度=“101” loc:SampleBehavior.SampleValue="201" /> <GridViewColumn 标题="姓氏" 宽度=“102” loc:SampleBehavior.SampleValue="202" /> </GridView> </ListView.View> </列表视图> </DockPanel> </窗口>
示例行为.cs
使用 System.Windows; 使用 System.Windows.Controls; 命名空间 Problem_Sample1 { 公共静态类 SampleBehavior { 公共静态只读 DependencyProperty SampleValueProperty = DependencyProperty.RegisterAttached( "样本值", 类型(int), typeof (SampleBehavior)); [AttachedPropertyBrowsableForType(typeof(GridViewColumn))] 公共静态 int GetSampleValue(GridViewColumn 列) { 返回 (int)column.GetValue(SampleValueProperty); } [AttachedPropertyBrowsableForType(typeof(GridViewColumn))] 公共静态无效 SetSampleValue(GridViewColumn 列,int 值) { column.SetValue(SampleValueProperty, 值); } } }
感谢您的任何帮助或建议。