从触发器绑定前景色

时间:2015-02-24 13:59:54

标签: wpf xaml checkbox

我希望将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>

任何想法如何让它发挥作用以及它为什么不起作用?

1 个答案:

答案 0 :(得分:1)

似乎触发器中的RelativeSource查找不起作用。您可以通过在CheckBox样式中定义本地路径样式来解决该限制:

<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时,您不需要额外的触发器。