Привет, у меня проблема с привязкой в ComboBox. Я хотел бы привязать элементы ComboBox к столбцам ListView и в качестве выбранного значения возвращать значение присоединенного свойства, определенного в выбранном столбце.
В приведенном ниже примере вы можете увидеть рабочий образец, отображающий ширину выбранного столбца. Если вы попытаетесь изменить SelectedValuePath в ComboBox на (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);
}
}
}
Спасибо за любую помощь или предложение.