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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18108648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:18:38  来源:igfitidea点击:

WPF Adding a custom property in a control

c#wpfxamlpropertiescustom-controls

提问by oimitro

Ok i have a WPF application in which i have a resource dictionary. In the dictionary i have a new style for a Buttonthat looks like this :

好的,我有一个 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>

And use this stye in my XAML like this :

并在我的 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"/>

Now what i want to do is, Create a new property in the Buttonstyle named OverWidthand OverHeightin order to use it like this :

现在我想要做的是,创造的一个新的属性Button命名样式OverWidthOverHeight以使用它像这样:

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

And in XAML :

在 XAML 中:

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

I dont even know if this is possible. I dont want to use Binding SomeIntengerfrom C# code. Thanks in advance.

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

采纳答案by Vyacheslav Volkov

You can use the 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:

Xml:

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"/>

回答by Sergio Perez

I used the Tag property, which works very well. I can write anything on Tag

我使用了Tag 属性,效果很好。我可以在标签上写任何东西

For example (AXML) on a Button:

例如 (AXML) 在Button 上

<Button x:Name="Btn80" Click="eventoClick" Tag="80">Casillero 80</Button>}

I can use a string field to add listsor valuesint, date, etc to then interpretand convertthe string into anything.

我可以使用字符串字段添加列表intdate等,然后字符串解释转换为任何内容。

**I used this solution, due to the need to add an ID field to a button.

**我使用了这个解决方案,因为需要给按钮添加一个 ID 字段。