none
请问是否可以通过触发器控制Image是否需要Clip属性? RRS feed

  • 问题

  • 我做了一个ListBoxItem的模版,里面含有Image控件,代码如下:

                                <Image Width="{Binding Path=ImageWidth,RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={ x:Type ListBox}}}" Source="{Binding Portrait}">
                                    <Image.Clip>
                                        <GeometryGroup FillRule="Nonzero">
                                            <EllipseGeometry RadiusX="{Binding ImageClipRadius,RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={ x:Type ListBox}}}" RadiusY="{Binding ImageClipRadius,RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={ x:Type ListBox}}}" Center="{Binding ImageClipCenter,RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={ x:Type ListBox}}}"></EllipseGeometry>
                                        </GeometryGroup>
                                    </Image.Clip>
                                </Image>

    Image的Clip属性,我希望可以做成通过属性、或者触发器去选择是否需要它,而不是像上面代码写死了一定会有Clip属性。

    请问是否可以实现这个需求?



    da jia hao!

    2018年6月6日 3:12

答案

  • 你好,

    下面的代码,你可以参考一下。

    <Window x:Class="ColeWPFSample.ImageSample.Window1"
            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:ColeWPFSample.ImageSample"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
        <Window.Resources>
            <Style x:Key="NormalStyle" TargetType="Image">
                <Style.Triggers>
                    <Trigger Property="Control.IsMouseOver" Value="true">
                        <Setter Property="Clip">
                            <Setter.Value>
                                <GeometryGroup FillRule="Nonzero">
                                    <EllipseGeometry Center="150,160" RadiusX="120" RadiusY="120">
                                    </EllipseGeometry>
                                </GeometryGroup>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <Grid>
            <Image Source="C:\Users\leow\Pictures\Data\2018050401.png" Style="{StaticResource NormalStyle}">
               
            </Image>
        </Grid>
    </Window>
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 liubin 2018年6月8日 8:32
    2018年6月7日 8:31