c# - WPF 在控件中添加自定义属性

标签 c# wpf xaml properties custom-controls

好的,我有一个 WPF 应用程序,其中有一个资源字典。在字典中,我有一个新的 Button 样式,如下所示:

    <Style x:Key="MenuButtonStyle" TargetType="Button">
      <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Grid>
                    <Image x:Name="Imager" RenderOptions.BitmapScalingMode="HighQuality" Width="{TemplateBinding Width}"  Height="{TemplateBinding Height}" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="UniformToFill"/>
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="Imager" Property="Width" Value="24" />
                        <Setter TargetName="Imager" Property="Height" Value="24" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Imager" Property="Width" Value="16" />
                        <Setter TargetName="Imager" Property="Height" Value="16" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后像这样在我的 XAML 中使用这个麦粒肿:

<Button x:Name="Home" Style="{DynamicResource MenuButtonStyle}" Width="20" Height="20" Tag="\Images\Home.png"/>
<Button x:Name="Settings" Style="{DynamicResource MenuButtonStyle}" Width="20" Height="20" Tag="\Images\Settings.png"/>

现在我想做的是,在 Button 样式中创建一个名为 OverWidthOverHeight 的新属性,以便像这样使用它这个:

<Trigger Property="IsMouseOver" Value="true">
   <Setter TargetName="Imager" Property="Width" Value= OVERWIDTH/>
   <Setter TargetName="Imager" Property="Height" Value= OVERHEIGHT/>
</Trigger>

在 XAML 中:

<Button x:Name="Home" Style="{DynamicResource MenuButtonStyle}" Width="20" Height="20" Tag="\Images\Home.png" OVERWIDTH = "24"/>

我什至不知道这是否可能。我不想在 C# 代码中使用 Binding SomeIntenger。 提前致谢。

最佳答案

您可以使用 attached properties :

public class Extensions
{
    public static readonly DependencyProperty OverWidthProperty =
        DependencyProperty.RegisterAttached("OverWidth", typeof(double), typeof(Extensions), new PropertyMetadata(default(double)));

    public static void SetOverWidth(UIElement element, double value)
    {
        element.SetValue(OverWidthProperty, value);
    }

    public static double GetOverWidth(UIElement element)
    {
        return (double)element.GetValue(OverWidthProperty);
    }
}

Xaml:

xmlns:extensions="clr-namespace:NAMESPACE FOR Extensions class"

<Trigger Property="IsMouseOver" Value="true">
    <Setter Property="MinWidth" Value="{Binding Path=(extensions:Extensions.OverWidth), RelativeSource={RelativeSource Self}}"/>
</Trigger>

<Button extensions:Extensions.OverWidth="100" Width="10"/>

关于c# - WPF 在控件中添加自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18108648/

相关文章:

表示 JSON 数据的 C# 类

c# - LeftBarButtonItem 和 RightBarButtonItem 未显示在 MonoTouch.Dialog DialogViewController 中

c# - 如何访问代码隐藏文件(xaml.cs)之外的样式?

c# - 如何保存和使用应用程序的窗口大小?

wpf - 如何实现击穿之类的效果

WPF:XAML 中的绑定(bind)列表 - 项目如何知道其在列表中的位置?

c# - 笔刷动画

c# - 如何从 Unity 映射中删除(取消注册)已注册的实例?

xaml - 如何在导航栏中心设置标题 Xamarin.forms

c# - 项目控制 : Change Property Of Control in ItemControl Based On "Content" of Control