我创建了一个用户控件(FilterPicker
),它将某个列表作为属性。这是一个依赖项属性,因此可以在使用用户控件时进行设置。
public static readonly DependencyProperty StrategiesProperty = DependencyProperty.Register(
"Strategies",
typeof(List<FilterType>),
typeof(FilterPicker),
new FrameworkPropertyMetadata
{
DefaultValue = new List<FilterType> { FilterType.Despike },
PropertyChangedCallback = StrategiesChangedCallback,
BindsTwoWayByDefault = false,
});
然后我尝试在我使用此控件的.xaml文件中定义此列表。
<Filtering:FilterPicker Grid.Row="1" Strategy="{Binding Strategy}">
<Filtering:FilterPicker.Strategies>
<Filtering1:FilterType>PassThrough</Filtering1:FilterType>
<Filtering1:FilterType>MovingAverage</Filtering1:FilterType>
<Filtering1:FilterType>Despike</Filtering1:FilterType>
</Filtering:FilterPicker.Strategies>
</Filtering:FilterPicker>
但是,它不起作用。 StrategiesChangedCallBack
永远不会被调用。
如果我通过绑定设置它,它工作正常 - 只是当我尝试在xaml中定义它时。所以这有效:
<Filtering:FilterPicker Grid.Row="1" Strategy="{Binding Strategy}" Strategies="{Binding AllStrategies}">
但不是之前的片段。关于我做错什么的任何想法?
答案 0 :(得分:2)
根据我原来问题的评论,我能够把它拼凑起来:
最后我改变了我的DependencyProperty以使用IEnumerable,我将其定义为使用FilterPicker UserControl的.xaml中的StaticResource。
的DependencyProperty:
public static readonly DependencyProperty StrategiesProperty = DependencyProperty.Register(
"Strategies",
typeof(IEnumerable<FilterType>),
typeof(FilterPicker),
new FrameworkPropertyMetadata
{
DefaultValue = ImmutableList<FilterType>.Empty, //Custom implementation of IEnumerable
PropertyChangedCallback = StrategiesChangedCallback,
BindsTwoWayByDefault = false,
});
使用它:
<Grid.Resources>
<x:Array x:Key="FilterTypes" Type="{x:Type Filtering1:FilterType}" >
<Filtering1:FilterType>PassThrough</Filtering1:FilterType>
<Filtering1:FilterType>MovingAverage</Filtering1:FilterType>
<Filtering1:FilterType>Fir</Filtering1:FilterType>
</x:Array>
</Grid.Resources>
<Filtering:FilterPicker Grid.Row="1" Strategies="{StaticResource FilterTypes}" />