【发布时间】:2016-05-02 10:24:36
【问题描述】:
我有一个WPF custom Control,其中包含一个ComboBox。我想通过CollectionViewSource将此组合框的ItemsSource绑定到自定义控件类的Dependency Property,但我不知道如何让CollectionViewSource识别正确的DataContext(我的自定义控制属性,在这种情况下)。
我搜索了很多,阅读了几乎所有 SO 自定义控件\CollectionViewSource\Collection binding\dependency properties binding\etc。问题并尝试了一些类似问题的解决方案,例如this one和this和somemore,但它仍然无法正常工作。我可能错过了什么,但我不知道是什么。
以下是自定义控件类的相关部分:
public class CustomComboBox : Control
{
static CustomComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomComboBox), new FrameworkPropertyMetadata(typeof(CustomComboBox)));
}
public CustomComboBox()
{
CustomItems = new ObservableCollection<ComboBoxItem>();
DataContext = this;
}
internal ObservableCollection<ComboBoxItem> CustomItems
{
get { return (ObservableCollection<ComboBoxItem>)GetValue(CustomItemsProperty); }
set { SetValue(CustomItemsProperty, value); }
}
// Using a DependencyProperty as the backing store for CustomItems. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CustomItemsProperty =
DependencyProperty.Register("CustomItems", typeof(ObservableCollection<ComboBoxItem>), typeof(CustomComboBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
private string text;
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged("Text");
}
}
// More properties, events and functions...
}
以及xaml代码的相关部分:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
x:Class="CustomComboBox">
<CollectionViewSource x:Key="GroupedData"
Source="{Binding Path=CustomItems, RelativeSource={RelativeSource FindAncestor, AncestorType=local:CustomComboBox}, diag:PresentationTraceSources.TraceLevel=High}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="GroupName" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<!-- ...Some more Styles and DataTemplates... -->
<Style TargetType="{x:Type local:CustomComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomComboBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ComboBox IsEditable="True"
Text="{Binding Text}"
ItemsSource="{Binding Source={StaticResource GroupedData}}">
<!-- ...Some more properties... -->
</ComboBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我从System.Diagnostics得到这个输出:
System.Windows.Data 警告:58:路径:'CustomItems'
System.Windows.Data 警告:60:BindingExpression (hash=28932383):默认模式解析为 OneWay
System.Windows.Data 警告:61:BindingExpression (hash=28932383):默认更新触发器解析为 PropertyChanged
System.Windows.Data 警告:62:BindingExpression (hash=28932383):附加到 System.Windows.Data.CollectionViewSource.Source (hash=23914501)
System.Windows.Data 警告:66:BindingExpression (hash=28932383):RelativeSource (FindAncestor) 需要树上下文
System.Windows.Data 警告:65 : BindingExpression (hash=28932383): Resolve source deferred
System.Windows.Data 警告:67:BindingExpression (hash=28932383):解析源
System.Windows.Data 警告:70:BindingExpression (hash=28932383):找到数据上下文元素:(OK)
System.Windows.Data 警告:67:BindingExpression (hash=28932383):解析源
System.Windows.Data 警告:70:BindingExpression (hash=28932383):找到数据上下文元素:(OK)
System.Windows.Data 警告:67:BindingExpression (hash=28932383):解析源(最后机会)
System.Windows.Data 警告:70:BindingExpression (hash=28932383):找到数据上下文元素:(OK)
System.Windows.Data 错误:4:找不到与引用“RelativeSource FindAncestor,AncestorType='MyNamespace.CustomComboBox',AncestorLevel='1''的绑定源。 BindingExpression:Path=CustomItems;数据项=空;目标元素是“CollectionViewSource”(HashCode=23914501);目标属性是“源”(类型“对象”)
我首先尝试了这个绑定:
<CollectionViewSource x:Key="GroupedData"
Source="{Binding CustomItems}">
然后我得到错误:
System.Windows.Data 错误:2:找不到管理 FrameworkElement 或 FrameworkContentElement 为目标元素。 BindingExpression:Path=CustomItems;数据项=空;目标元素是 'CollectionViewSource' (HashCode=37908782);目标属性是 “源”(类型“对象”)
顺便说一句-在 ComboBox 内绑定到另一个属性的工作原理-类似于Text 绑定。
【问题讨论】:
标签: c# wpf data-binding custom-controls dependency-properties