简体   繁体   English

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

[英]Wpf Custom button icon and text change from code

So I'm gonna be short. 所以我要简短。 I would like to set the textBlock (x:Name="ButtonText") and Images (x:Name="LeftIcon" and x:Name="RightIcon") from code in another Project using the compiled .dll from this. 我想使用另一个从这里编译的.dll文件中的代码设置textBlock(x:Name =“ ButtonText”)和图像(x:Name =“ LeftIcon”和x:Name =“ RightIcon”)。

XAML: 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: 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();
        }
    }

}

Currently if I append a new textBlock to my button instance from code, it doesn't show up... Same for the images. 目前,如果我将新的textBlock从代码追加到我的按钮实例中,则不会显示...对于图像相同。 What am I missing? 我想念什么?

Thank you for the help... :) 感谢您的帮助... :)

Your actionButton class should declare bindable dependency properties for the two images and the text. 您的actionButton类应为两个图像和文本声明可绑定的依赖项属性。

An example for the left image is shown below. 左图的示例如下所示。 The property type is ImageSource , because that is the type of the Source property of the Image control. 该属性类型为ImageSource ,因为这是Image控件的Source属性的类型。 You would have to declare a second property for the right image, and a property of type string for the TextBlock text. 您将必须为正确的图像声明第二个属性,并为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); }
}

In the Button's XAML, you would use the properties with a RelativeSource Binding: 在Button的XAML中,您可以将属性与RelativeSource绑定一起使用:

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

Since these Bindings are in the Button's Template, you may as well use a TemplateBinding. 由于这些绑定位于Button的模板中,因此您也可以使用TemplateBinding。 You would however have to set the appropriate TargetType: 但是,您必须设置适当的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>

Now when you use your Button, you can simply set (or also bind) these properties like this: 现在,当您使用Button时,您可以像这样简单地设置(或绑定)这些属性:

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

or in code behind: 或在后面的代码中:

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

If the image file should be embedded as assembly resource, you should set its Build Action to Resource, and load it from a Resource File Pack URI : 如果图像文件应作为程序集资源嵌入,则应将其Build Action设置为Resource,然后从Resource File Pack URI加载它:

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

As a note, you should follow widely accepted naming conventions, and call it ActionButton . 请注意,您应该遵循广泛接受的命名约定,并将其命名为ActionButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM