私はボタンのカスタムコントロールを使用して次のコードをリファクタリングしたいと思います。テキストとアイコンのあるボタンのWPFカスタム/ユーザーコントロール。どちらを選ぶべきですか?
XAMLファイル:背後LoginWindow.xaml
<Button Grid.Column="0" Style="{DynamicResource BlackLoginButton}" Click="btnLogin_Click" IsDefault="True">
<DockPanel>
<TextBlock Text="text1" VerticalAlignment="Center"/>
<Image Source="../../../Assets/Images/apply.png" Style="{StaticResource ImageButton}" />
</DockPanel>
</Button>
コード:
<local:CustomButton>
Grid.Column="0"
IsDefault="True"
Style="{DynamicResource BlackLoginButton}"
Click="btnLogin_Click"
Text="ciao"
Image="../../../Assets/Images/apply.png">
</local:CustomButton>
私が使用しよう:私は特に、私はこのような構造を持っているしたいと思いますLoginWindow.xaml.cs
private void btnLogin_Click(object sender, EventArgs e)
{
Login();
}
カスタムコントロールとユーザーコントロールの両方。ここで
は私のUserControl
XAML
<UserControl x:Class="foo.View.CustomUserControl.IconButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Button Style="{DynamicResource BlackLoginButton}">
<DockPanel>
<TextBlock VerticalAlignment="Center" Text="{Binding ElementName=Button, Path=Text}" />
<Image Source="{Binding ElementName=Button, Path=Image}" Style="{StaticResource ImageButton}" />
</DockPanel>
</Button>
</UserControl>
そして、背後にあるコードです:
using System.Windows;
using System.Windows.Media;
namespace Mhira3D.View.CustomUserControl
{
public partial class IconButton
{
public static DependencyProperty ClickProperty = DependencyProperty.Register("Click", typeof(RoutedEventHandler),
typeof(IconButton));
public static DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource),
typeof(IconButton));
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(IconButton));
public IconButton()
{
InitializeComponent();
}
public RoutedEventHandler Click
{
get { return (RoutedEventHandler) GetValue(ClickProperty); }
set { SetValue(ClickProperty, value); }
}
public ImageSource Image
{
get { return (ImageSource) GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
public string Text
{
get { return this.GetValue(TextProperty) as string; }
set { this.SetValue(TextProperty, value); }
}
}
}
この構造の主な問題は、私はButton
プロパティを使用することができないということです(私はボタンから継承していませんでした)、たとえば、私はIsDefault
を使用できません。
私はそれがこの(THI例では、私が唯一Image
プロパティを置く)のように、より良い方法でボタンプロパティを使用して、代替使用CustomControl
ことができると思います:
public class IconButtonCustom : Button
{
static IconButtonCustom()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(IconButtonCustom), new FrameworkPropertyMetadata(typeof(IconButtonCustom)));
}
public ImageSource Image
{
get { return GetValue(SourceProperty) as ImageSource; }
set { SetValue(SourceProperty, value); }
}
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Image", typeof(ImageSource), typeof(IconButtonCustom));
}
とスタイルでGeneric.xaml
<Style TargetType="{x:Type customUserControl:IconButtonCustom}" BasedOn="{StaticResource BlackLoginButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type customUserControl:IconButtonCustom}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<TextBlock VerticalAlignment="Center" Text="{Binding ElementName=Button, Path=Text}" />
<Image Source="{Binding ElementName=Button, Path=Image}" Style="{StaticResource ImageButton}" />
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
今、私はいくつかの質問を持っている:
- カスタムボタンの作成に適した方法はどれですか?
- 両方のメソッドを使用すると、実際には
System.Windows.Markup.XamlParseException
を取得しましたが、WPFは文字列btnLogin_Click
を関数Click
に変換できないため、クリック機能のマッピングに問題があります。 Generic.xaml
は一度も使用していませんが、私の例は正しいですか、何らかの方法で改善する必要がありますか?
インスタンスごとに異なるイメージを渡す方法を探していますか? –
異なる画像、テキスト、クリック機能;) – Rowandish
カスタムコントロールを追加する特別な理由はありますか?ボタンスタイルテンプレートだけですべてを行うことができるからです。 –