2017-10-01 11 views
0

だから私は短くなるだろう。私はtextBlock(x:Name = "ButtonText")とImages(x:Name = "LeftIcon"とx:Name = "RightIcon")を別のプロジェクトのコードから設定したいと思います。Wpfカスタムボタンのアイコンとコードからのテキストの変更

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

} 
現在

私はコードから私のボタンインスタンスに新しいテキストブロックを追加する場合は、同じ...表示されません画像のために。私は何が欠けていますか?

は助けてくれてありがとう... :)

答えて

1

をごactionButtonクラスは、二つの画像及びテキストのバインド可能な依存関係プロパティを宣言する必要があります。

左の画像の例を以下に示します。プロパティタイプはImageSourceです。これはImageコントロールのSourceプロパティのタイプです。正しいイメージの2番目のプロパティを宣言し、TextBlockテキストの型としてstringのプロパティを宣言する必要があります。ボタンのXAMLで

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); } 
} 

、あなたはバインディング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}}" ... /> 

をこれらのバインディングは、ボタンのテンプレートであるので、あなたにもTemplateBindingのを使用することができます。

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

かで:あなたは、単に(バインドもか)このように、これらのプロパティを設定することができ、あなたのボタンを使用するときに今

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

:ただし、適切なTargetTypeに設定する必要がありますコードの後ろに:

イメージファイルをアセンブリリソースとして埋め込む必要がある場合は、そのビルドアクションをResou RCE、およびResource File Pack URIからそれをロードします。注意点としては

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

、あなたは広く受け入れられている命名規則に従わなければならない、とActionButtonそれを呼び出します。

+0

ありがとうございます!それは魅力的なように働いています... :)(いくつかの変更を加えて):) –

関連する問題