【发布时间】:2017-05-26 13:40:28
【问题描述】:
我有一个 WPF MarkupExtension 用于修改 ListBoxItem 属性,例如背景。此背景新值基于 ListBoxItem.DataContext 和 MarkupExtension 属性,例如 Color。
<ListBox ItemsSource="{Binding ColorfullItems}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="{Helpers:MyMarkupExtension Color=Blue}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
我的 MarkupExtension 是:
public class MyMarkupExtension : MarkupExtension
{
public string Color { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget provideValueTarget = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
// The following target is a Setter instance,
// which doesn't have ListBoxItem.DataContext property.
var target = provideValueTarget.TargetObject;
// I put break point here.
// **How can I get ListBoxItem.DataContext instance here?**
}
}
1.如何在 ProvideValue 覆盖方法中获取 ListBoxItem.DataContext(它必须是 ColorfullItem 实例)?
2. 这可能吗?
【问题讨论】:
标签: c# .net wpf xaml markup-extensions