0

我创建了一个自定义图像按钮并添加了一些样式。一切正常。但是当我尝试在其他按钮中重用它时,它并没有很好地工作。只有 1 个按钮会遵循样式,其他按钮始终为空白。

我在运行时没有任何错误。请帮忙!!

谢谢

这是代码:

按钮:

    public class ImageTextButton : Button
{
    public DependencyProperty TextProperty { get; set; }
    public DependencyProperty ImageProperty { get; set; }

    public ImageTextButton()
    {
        TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ImageTextButton), null);
        ImageProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageTextButton), null);
    }

    public ImageSource ImageSource
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    public string Text { get; set; }
}

xml:

<Controls:ImageTextButton HorizontalAlignment="Left" Command="{Binding SendCustomerChanges}" Style="{StaticResource ImageTextButton}" IsEnabled="{Binding Path=DetailsInformation.IsAllowSave}"
                             Text="{Binding Path=CustomerResources.Button_CloseDetailedView, Source={StaticResource ResourceWrapper}}" ImageSource="{Binding Path=SaveIcon, Source={StaticResource ApplicationIcons}}" />

风格:

    <Style TargetType="Controls:ImageTextButton" x:Key="ImageTextButton" >
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Controls:ImageTextButton">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommomStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Duration="0" To="LightGray" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled" >
                                <Storyboard>
                                    <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
                                    <ColorAnimation Duration="0" To="#AAAAAA" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Rectangle x:Name="rectangle" Fill="#E0E9F1"  StrokeThickness="1" Stroke="#728DA3"/>
                    <Rectangle x:Name="DisabledVisualElement" Fill="#F5F5F5" IsHitTestVisible="false" StrokeThickness="1" Stroke="#D3D3D3" Opacity="0" />
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Image Grid.Column="0" Width="12" Height="12" Margin="2,0,2,0" Source="{TemplateBinding ImageSource}">
                        </Image>
                        <TextBlock Grid.Column="1" x:Name="textBlock" Margin="2,3,4,0" Text="{TemplateBinding Text}" />
                    </Grid>
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5,3,5,3"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

1 回答 1

3

您需要更改/修复一些事情。

  1. 您的文本依赖属性格式不正确。您必须为 DP 定义一个 getter 和 setter(就像您对 ImageSourceProperty 所做的那样)。

  2. 您的 ImageSource 依赖属性也需要一些注意... DP 应命名为 ImageSourceProperty ...Dependency Properties must follow the pattern 描述here if they are to work under all conditions. Especially so when you are setting them from XAML

  3. 您需要一个default style key新课程,以便实际应用您的样式

  4. Your dependency properties should be registered in a static constructor (or as a field initializer. Not in the instance constructor.

    public class ImageTextButton : Button
    {
        public ImageTextButton()
        {
            DefaultStyleKey = typeof(ImageTextButton);
        }
    
        static ImageTextButton()
        {
            TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ImageTextButton), new PropertyMetadata(null));
            ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageTextButton), new PropertyMetadata(null));
        }
    
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty;
    
    
    
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty;
    
    
    }
    
于 2013-03-05T07:32:14.647 回答