Nic*_*ler 5 data-binding wpf xaml
我有一个MenuItem带有控制模板的控件模板,可以像这样MenuItem更改Foreground:
<ControlTemplate TargetType="MenuItem">
<StackPanel Background="{Binding Background}" Orientation="Horizontal">
<ContentPresenter Content="{TemplateBinding Icon}"/>
<TextBlock Text="{TemplateBinding Header}"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我想将图标的颜色绑定到该前景色。在这种情况下,图标只是一个圆圈。我尝试了以下绑定:
<MenuItem Header="Sub">
<MenuItem.Icon>
<Ellipse Width="16" Height="16" Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=MenuItem}}"/>
</MenuItem.Icon>
</MenuItem>
Run Code Online (Sandbox Code Playgroud)
但是,这会在应用程序启动时出现以下绑定错误,并且椭圆最终不会被渲染:
System.Windows.Data 错误:4:找不到引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.MenuItem',AncestorLevel='1”的绑定源。BindingExpression:Path=前景;数据项=空;目标元素是“椭圆”(名称=“”);目标属性是“填充”(类型“画笔”)
我还尝试在图标中放置一个虚拟元素,以传播前景并绑定到此:
<MenuItem.Icon>
<Grid>
<TextBlock Name="foregroundCapture"/>
<Ellipse Width="16" Height="16" Fill="{Binding Foreground, ElementName=foregroundCapture}"/>
</Grid>
</MenuItem.Icon>
Run Code Online (Sandbox Code Playgroud)
但这给出了类似的错误:
System.Windows.Data 错误:4:找不到引用“ElementName=foregroundCapture”的绑定源。BindingExpression:Path=前景;数据项=空;目标元素是“椭圆”(名称=“”);目标属性是“填充”(类型“画笔”)
我怎样才能使绑定工作?仅供参考,图标最终将是资源字典中的一个对象。因此它将无法访问实际的菜单项。
我可以想到几个替代解决方案。但实际上它们都不是很好:
MouseOverIcon并让触发器使用它。Foreground传播属性。这将允许直接在控件内进行直接绑定。<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="MenuItem">
<Style.Triggers>
<Trigger Property="Role" Value="SubmenuItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<StackPanel Background="{Binding Background}" Orientation="Horizontal">
<ContentPresenter Content="{TemplateBinding Icon}"/>
<TextBlock Text="{TemplateBinding Header}"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="Header">
<MenuItem Header="Sub">
<MenuItem.Icon>
<Grid>
<TextBlock Name="foregroundCapture"/>
<Ellipse Width="16" Height="16" Fill="{Binding Foreground, ElementName=foregroundCapture}"/>
</Grid>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>
<Grid>
</Grid>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
876 次 |
| 最近记录: |