开发者

How to define 'Attached property' as 'SelectedValuePath' in ComboBox?

Hi, I have a problem with binding in ComboBox. I'd like to bind ComboBox items to ListView columns and as a selected value return value of attached property defined on the selected column.

In example bellow you can see working sample that displays width of selected column. If you try to change SelectedValuePath in ComboBox into (loc:SampleBehavior.SampleValue) you get binding error:

BindingExpression path error: '(u:SearchableListView.SearchMemberPath)' property not found on 'object' ''GridViewColumn'

<Window 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-namespace:Problem_Sample1"
  WindowStartupLocation="CenterScreen"
  Title="Window1" 
  Height="300" Width="300">
  <DockPanel>
    <ComboBox DockPanel.Dock="Top"
         x:Name="combobox"
         ItemsSource="{Binding Path=View.Columns, ElementName=listview}"
         DisplayMemberPath="Header"
         SelectedValuePath="Width">
    </ComboBox>

    <StatusBar DockPanel.Dock="Bottom">
      <TextBlock>
        <TextBlock Text="Selected column (value): " />
        <TextBlock Text="{Binding Path=SelectedValue, ElementName=combobox}" />
      </TextBlock>
    </StatusBar>

    <ListView x:Name="listview">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Name" 
                  Width="101" 
                  loc:SampleBehavior.SampleValue="201" />
          <GridViewColumn Header="Surname" 
                  Width="102" 
                  loc:SampleBehavior.SampleValue="202" />
        </GridView>
      </ListView.View>
    </ListView>
  </DockPanel>
</Window>

 

SampleBehavior.cs

using System.Windows;
using System.Windows.Controls;

namespace Problem_Sample1
{
  public static class SampleBehavior
  {

    public static readonly DependencyProperty SampleValueProperty =
      DependencyProperty.RegisterAttached(
        "SampleValue",
        typeof (int),
        typeof (SampleBehavior));

    [AttachedPropertyBrowsableForType(typeof(GridViewColumn))]
    public static int GetSampleValue(GridViewColumn column)
    {
      return (int)column.GetValue(SampleValueProperty);
    }

    [AttachedPropertyBrowsableForT开发者_C百科ype(typeof(GridViewColumn))]
    public static void SetSampleValue(GridViewColumn column, int value)
    {
      column.SetValue(SampleValueProperty, value);
    }

  }
}

 

Thanks for any help or suggestion.


Since I stumbled upon this (and it's the first google result for some reasonable search), I might as well write an answer now.

The requested functionality is actually available exactly the way it was asked.

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

It is important to put the attached property path in (braces), otherwise it will try some weird lookup on the source object.

Also, the error message in the question states "BindingExpression path error: '(u:SearchableListView.SearchMemberPath)' property not found on 'object' ''GridViewColumn'", so the error message is definitely related to a completely different property and not to "(loc:SampleBehavior.SampleValue)". This inconsistency seems to be a problem related to the edit that was done in order to reduce the code sample

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜