Wpf自定义按钮图标和文本从代码更改

时间:2017-10-01 14:29:37

标签: c# wpf xaml

所以我会变短。我想使用此编译的.dll从另一个Project中的代码设置textBlock(x:Name =“ButtonText”)和Images(x:Name =“LeftIcon”和x:Name =“RightIcon”)。

XAML:

<Button x:Class="myClass.actionButton"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   x:Name="ActionButton">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="button" CornerRadius="10" BorderBrush="#F555" BorderThickness="1.5">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#CC323232" Offset="0"/>
                        <GradientStop Color="#CC4B4B4B" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="24"/>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="24"/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="ButtonText" Grid.Column="1" HorizontalAlignment="Center" TextWrapping="Wrap" Margin="0,0,0,0" VerticalAlignment="Center" Width="auto"/>
                <Image x:Name="LeftIcon" Grid.Column="0" HorizontalAlignment="Left" Height="16" Margin="4,1,0,0" VerticalAlignment="Center" Width="16"/>
                <Image x:Name="RightIcon" Grid.Column="2" HorizontalAlignment="Right" Height="16" Margin="0,1,4,0" VerticalAlignment="Center" Width="16"/>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace myClass
{
    public partial class actionButton : Button
    {
        private TextBlock ButtonText;
        private Image LeftIcon;
        private Image RightIcon;

        public Image leftIcon
        {
            get { return LeftIcon; }
            set { LeftIcon = value; }
        }

        public Image rightIcon
        {
            get { return RightIcon; }
            set { RightIcon = value; }
        }

        public TextBlock buttonText
        {
            get { return ButtonText; }
            set { ButtonText = value; }
        }

        public actionButton()
        {
            InitializeComponent();
        }
    }

}

目前,如果我从代码中将新的textBlock附加到我的按钮实例,则它不会显示...图像相同。我错过了什么?

感谢您的帮助......:)

1 个答案:

答案 0 :(得分:1)

您的actionButton类应声明两个图像和文本的可绑定依赖项属性。

左图的示例如下所示。属性类型为ImageSource,因为这是Image控件的Source属性的类型。您必须为右侧图像声明第二个属性,并为TextBlock文本声明类型为string的属性。

public static readonly DependencyProperty LeftImageProperty =
    DependencyProperty.Register(
        nameof(LeftImage), typeof(ImageSource), typeof(actionButton));

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

在Button的XAML中,您将使用具有RelativeSource Binding的属性:

<TextBlock Text="{Binding Text,
                  RelativeSource={RelativeSource AncestorType=Button}}" ... />
<Image Source="{Binding LeftImage,
                RelativeSource={RelativeSource AncestorType=Button}}" ... />
<Image Source="{Binding RightImage,
                RelativeSource={RelativeSource AncestorType=Button}}" ... />

由于这些绑定位于Button的模板中,因此您也可以使用TemplateBinding。但是,您必须设置适当的TargetType:

<Button x:Class="MyNamespace.actionButton"
        ...
        xmlns:local="clr-namespace:MyNamespace">
    <Button.Template>
        <ControlTemplate TargetType="local:actionButton">
            ...
            <Image Source="{TemplateBinding LeftImage}" ... />
            ...
        </ControlTemplate>
    </Button.Template>
</Button>

现在,当您使用Button时,您可以简单地设置(或绑定)这些属性,如下所示:

<local:ActionButton x:Name="ab" LeftImage="SomeImage.jpg"/>

或代码背后:

ab.LeftImage = new BitmapImage(new Uri("SomeImage.jpg", UriKind.RelativeOrAbsolute));

如果图像文件应作为程序集资源嵌入,则应将其Build Action设置为Resource,并从Resource File Pack URI加载它:

ab.LeftImage = new BitmapImage(new Uri("pack://application:,,,/SomeImage.jpg"));

作为一个注释,您应该遵循广泛接受的命名约定,并将其命名为ActionButton