代码之家  ›  专栏  ›  技术社区  ›  B.K.

为每个状态单独的图像使用依赖项财产的按钮

  •  1
  • B.K.  · 技术社区  · 10 年前

    我有一个 Button 我希望能够在整个项目中重用的控件。每次按钮进入新状态时,将显示不同的图像。现在,我有 Normal State Pressed State .

    下面是控件的XAML部分:

    <Button
        x:Class="customImageButton.ImageButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Button.Template>
          <ControlTemplate TargetType="{x:Type Button}">
             <Grid>
                <ContentControl Width="80">
                    <Grid>
                        <Image Name="Normal" Source="{Binding NormalState}"/>
                        <Image Name="Pressed" Source="{Binding PressedState}" Visibility="Hidden"/>
                    </Grid>
                </ContentControl>
             </Grid>
             <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                   <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
                   <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
                </Trigger>
             </ControlTemplate.Triggers>
          </ControlTemplate>
       </Button.Template>
    </Button>
    

    下面是控件的代码:

    namespace customImageButton
    {
        public partial class ImageButton : Button
        {
            public ImageButton()
            {
                this.InitializeComponent();
            }
    
            public ImageSource NormalState
            {
                get { return base.GetValue(NormalStateProperty) as ImageSource; }
                set { base.SetValue(NormalStateProperty, value); }
            }
    
            public static readonly DependencyProperty NormalStateProperty =
                DependencyProperty.Register("NormalState", typeof(ImageSource), typeof(ImageButton));
    
            public ImageSource PressedState
            {
                get { return base.GetValue(PressedStateProperty) as ImageSource; }
                set { base.SetValue(PressedStateProperty, value); }
            }
    
            public static readonly DependencyProperty PressedStateProperty =
                DependencyProperty.Register("PressedState", typeof(ImageSource), typeof(ImageButton));
        }
    }
    

    …其用途如下:

    <local:ImageButton Content="CustomButton" HorizontalAlignment="Left" 
                       VerticalAlignment="Top" NormalState="Resources/Normal.png"
                       PressedState="Resources/Pressed.png"/>
    

    我的问题是我提供的图像无法显示。这个 Build Action 对于这两个图像 Resource 我尝试过使用绝对路径;然而,这提供了相同的结果。我错过了什么?

    1 回复  |  直到 10 年前
        1
  •  1
  •   McGarnagle    10 年前

    列出的代码有两个问题:

    1. 绑定需要是 TemplateBinding .
    2. 这个 TargetType 应引用“ImageButton”类型,而不是 Button .

    这样地:

    <ControlTemplate 
         xmlns:local="clr-namespace:customImageButton"
         TargetType="{x:Type local:ImageButton}">
         <Grid>
            <ContentControl Width="80">
                <Grid>
                    <Image Name="Normal" Source="{TemplateBinding NormalState}"/>
                    <Image Name="Pressed" Source="{TemplateBinding PressedState}" Visibility="Hidden"/>
                </Grid>
            </ContentControl>
         </Grid>
    

    注: 以上构建并运行,但Visual Studio抱怨:

    “ImageButton”ControlTemplate TargetType与模板类型“Button”不匹配。

    我建议在 Themes/Generic.xaml 资源字典,而不是内联。