2010-06-10 22 views
0

嗨,我有一個與ComboBox綁定的問題。我想將ComboBox項綁定到ListView列,並作爲選定列上定義的附加屬性的選定值返回值。如何在ComboBox中將'Attached property'定義爲'SelectedValuePath'?

在示例波紋管中,您可以看到顯示選定列寬度的工作示例。如果你試圖改變SelectedValuePath在組合框進(LOC:SampleBehavior.SampleValue)你綁定錯誤:

BindingExpression路徑錯誤: '(U:SearchableListView.SearchMemberPath)' 屬性不是 '對象' 發現'' 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); 
    } 

    [AttachedPropertyBrowsableForType(typeof(GridViewColumn))] 
    public static void SetSampleValue(GridViewColumn column, int value) 
    { 
     column.SetValue(SampleValueProperty, value); 
    } 

    } 
} 

 

感謝任何幫助或建議。

回答

0

因爲我偶然發現了這個(這是谷歌搜索的第一個結果),所以我現在不妨寫一個答案。

請求的功能實際上可以完全按照它的要求提供。

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

把附加的屬性路徑放在(大括號)是很重要的,否則它會嘗試對源對象進行一些奇怪的查找。

此外,在該問題時的錯誤消息「BindingExpression路徑錯誤:‘(U:SearchableListView.SearchMemberPath)’屬性不是‘對象’‘’GridViewColumn」發現」,所以該錯誤信息是絕對相關一個完全不同的屬性,而不是「(loc:SampleBehavior.SampleValue)」。這種不一致似乎是與編輯有關的問題,這是爲了減少代碼示例

相關問題