从触发器绑定前景色
Binding foreground color from trigger
我希望 Path
作为 CheckBox
中的内容并且我希望 Fill
颜色是该复选框的 Foreground
颜色。最重要的是,我需要在选中 checkbox
时更改路径数据。
应用此样式的复选框加载时显示路径,但单击它时路径消失。我知道它在那里,因为当我检查它时我可以看到该复选框具有不同的宽度,但路径不可见。出于某种原因,当我用 snoop 检查时,一切似乎都很好。此外,当我摆脱绑定并选择例如黑色作为我在触发器中的路径的填充颜色时,它就可以工作了。
<Style TargetType="CheckBox">
<Setter Property="Content">
<Setter.Value>
<Path Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Data="F1 M 22.17,36.4216L 25.3369,36.4216L 25.3369,31.6711C 25.3369,24.6745 31.0087,19.0027 38.0053,19.0027C 45.0019,19.0027 50.6737,24.6745 50.6737,31.6711L 50.6737,36.4216L 53.841,36.4216L 53.8411,57.008L 22.17,57.008L 22.17,36.4216 Z M 45.9231,31.6711C 45.9231,27.2982 42.3782,23.7533 38.0053,23.7533C 33.6324,23.7533 30.0875,27.2982 30.0875,31.6711L 30.0875,36.4216L 45.923,36.4216L 45.9231,31.6711 Z "/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<Path Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Data="F1 M 52.2573,19.0027C 58.3793,19.0027 63.3422,23.9655 63.3422,30.0875L 63.3422,31.6711L 58.5915,31.6711L 58.5915,30.0875C 58.5915,26.5892 55.7556,23.7533 52.2573,23.7533C 48.759,23.7533 45.9231,26.5892 45.9231,30.0875L 45.923,34.8377L 49.0902,34.8377L 49.0902,55.4242L 19.0029,55.4242L 19.0029,34.8378L 41.1724,34.838L 41.1724,30.0875C 41.1724,23.9655 46.1353,19.0027 52.2573,19.0027 Z M 31.6711,50.6737L 36.4217,50.6737L 35.1052,44.0911C 35.8857,43.7018 36.4217,42.8956 36.4217,41.9642C 36.4217,40.6523 35.3583,39.5889 34.0464,39.5889C 32.7346,39.5889 31.6711,40.6523 31.6711,41.9642C 31.6711,42.8956 32.2072,43.7018 32.9876,44.0911L 31.6711,50.6737 Z "/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content">
<Setter.Value>
<Path Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Data="F1 M 22.17,36.4216L 25.3369,36.4216L 25.3369,31.6711C 25.3369,24.6745 31.0087,19.0027 38.0053,19.0027C 45.0019,19.0027 50.6737,24.6745 50.6737,31.6711L 50.6737,36.4216L 53.841,36.4216L 53.8411,57.008L 22.17,57.008L 22.17,36.4216 Z M 45.9231,31.6711C 45.9231,27.2982 42.3782,23.7533 38.0053,23.7533C 33.6324,23.7533 30.0875,27.2982 30.0875,31.6711L 30.0875,36.4216L 45.923,36.4216L 45.9231,31.6711 Z "/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
关于如何让它工作以及为什么它首先不起作用的任何想法?
触发器中的 RelativeSource 查找似乎不起作用。您可以通过在复选框样式中定义本地路径样式来解决该限制:
<Style TargetType="CheckBox">
<Style.Resources>
<Style TargetType="Path">
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="Fill" Value="{Binding Foreground,
RelativeSource={RelativeSource AncestorType=CheckBox}}"/>
</Style>
</Style.Resources>
<Setter Property="Content">
<Setter.Value>
<Path Data="..."/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<Path Data="..."/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
另请注意,当 IsChecked 为 false 时,您不需要额外的触发器。
我希望 Path
作为 CheckBox
中的内容并且我希望 Fill
颜色是该复选框的 Foreground
颜色。最重要的是,我需要在选中 checkbox
时更改路径数据。
应用此样式的复选框加载时显示路径,但单击它时路径消失。我知道它在那里,因为当我检查它时我可以看到该复选框具有不同的宽度,但路径不可见。出于某种原因,当我用 snoop 检查时,一切似乎都很好。此外,当我摆脱绑定并选择例如黑色作为我在触发器中的路径的填充颜色时,它就可以工作了。
<Style TargetType="CheckBox">
<Setter Property="Content">
<Setter.Value>
<Path Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Data="F1 M 22.17,36.4216L 25.3369,36.4216L 25.3369,31.6711C 25.3369,24.6745 31.0087,19.0027 38.0053,19.0027C 45.0019,19.0027 50.6737,24.6745 50.6737,31.6711L 50.6737,36.4216L 53.841,36.4216L 53.8411,57.008L 22.17,57.008L 22.17,36.4216 Z M 45.9231,31.6711C 45.9231,27.2982 42.3782,23.7533 38.0053,23.7533C 33.6324,23.7533 30.0875,27.2982 30.0875,31.6711L 30.0875,36.4216L 45.923,36.4216L 45.9231,31.6711 Z "/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<Path Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Data="F1 M 52.2573,19.0027C 58.3793,19.0027 63.3422,23.9655 63.3422,30.0875L 63.3422,31.6711L 58.5915,31.6711L 58.5915,30.0875C 58.5915,26.5892 55.7556,23.7533 52.2573,23.7533C 48.759,23.7533 45.9231,26.5892 45.9231,30.0875L 45.923,34.8377L 49.0902,34.8377L 49.0902,55.4242L 19.0029,55.4242L 19.0029,34.8378L 41.1724,34.838L 41.1724,30.0875C 41.1724,23.9655 46.1353,19.0027 52.2573,19.0027 Z M 31.6711,50.6737L 36.4217,50.6737L 35.1052,44.0911C 35.8857,43.7018 36.4217,42.8956 36.4217,41.9642C 36.4217,40.6523 35.3583,39.5889 34.0464,39.5889C 32.7346,39.5889 31.6711,40.6523 31.6711,41.9642C 31.6711,42.8956 32.2072,43.7018 32.9876,44.0911L 31.6711,50.6737 Z "/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content">
<Setter.Value>
<Path Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CheckBox}}}" Data="F1 M 22.17,36.4216L 25.3369,36.4216L 25.3369,31.6711C 25.3369,24.6745 31.0087,19.0027 38.0053,19.0027C 45.0019,19.0027 50.6737,24.6745 50.6737,31.6711L 50.6737,36.4216L 53.841,36.4216L 53.8411,57.008L 22.17,57.008L 22.17,36.4216 Z M 45.9231,31.6711C 45.9231,27.2982 42.3782,23.7533 38.0053,23.7533C 33.6324,23.7533 30.0875,27.2982 30.0875,31.6711L 30.0875,36.4216L 45.923,36.4216L 45.9231,31.6711 Z "/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
关于如何让它工作以及为什么它首先不起作用的任何想法?
触发器中的 RelativeSource 查找似乎不起作用。您可以通过在复选框样式中定义本地路径样式来解决该限制:
<Style TargetType="CheckBox">
<Style.Resources>
<Style TargetType="Path">
<Setter Property="Stretch" Value="Uniform"/>
<Setter Property="Fill" Value="{Binding Foreground,
RelativeSource={RelativeSource AncestorType=CheckBox}}"/>
</Style>
</Style.Resources>
<Setter Property="Content">
<Setter.Value>
<Path Data="..."/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<Path Data="..."/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
另请注意,当 IsChecked 为 false 时,您不需要额外的触发器。