0

嗨,我在 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, 值);
    }

  }
}

 

感谢您的任何帮助或建议。

4

1 回答 1

1

由于我偶然发现了这一点(这是进行一些合理搜索的第一个谷歌结果),我现在不妨写一个答案。

请求的功能实际上完全按照它被询问的方式可用。

<ComboBox DockPanel.Dock="Top"
     x:Name="combobox"
     ItemsSource="{Binding Path=View.Columns, ElementName=listview}"
     DisplayMemberPath="Header"
     SelectedValuePath="(loc:SampleBehavior.SampleValue)">

将附加的属性路径放在(大括号)中很重要,否则它将尝试对源对象进行一些奇怪的查找。

此外,问题中的错误消息指出“ BindingExpression path error: '(u:SearchableListView.SearchMemberPath)' property not found on 'object' ''GridViewColumn' ”,因此错误消息肯定与完全不同的属性有关,而不是到“ (loc:SampleBehavior.SampleValue) ”。这种不一致似乎是与为了减少代码示例而进行的编辑有关的问题

于 2016-12-22T14:36:05.270 回答