将属性绑定到整数

发布于 2024-10-27 11:14:37 字数 136 浏览 1 评论 0原文

我已经阅读了很多教程,但不知何故,他们提到将属性绑定到简单整数没有任何作用。

设置如下:

我有一个用户控件。 我想将“private int size”绑定到 XAML 文件内边框的宽度。

最简单的方法是什么?

I've been reading a lot tutorials but somehow nothing works they mention about binding a property to a simple integer.

Here's the setup:

I got a user-control.
I want to bind "private int size" to the width of a border within the XAML-file.

Whats the easiest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-11-03 11:14:37

与绑定其他任何内容的方式相同:

<Border BorderThickness="{Binding Size}">
private int _Size;
public int Size
{
    get { return _Size; }
    set 
    {
        _Size = value; 
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Size");
    }
}

当然,您的类也必须实现 INotifyPropertyChanged。

The same way you'd bind anything else:

<Border BorderThickness="{Binding Size}">
private int _Size;
public int Size
{
    get { return _Size; }
    set 
    {
        _Size = value; 
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Size");
    }
}

Of course your class must implement INotifyPropertyChanged as well.

说不完的你爱 2024-11-03 11:14:37

另一种方法是声明一个新的依赖属性并应用TemplateBinding

这是控件模板,我在其中设置将 Size 属性绑定到宽度。

<Style TargetType="{x:Type local:MyUserControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyUserControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox Width="{TemplateBinding Size}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>



public class MyUserControl : Control
{
    static MyUserControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyUserControl), new FrameworkPropertyMetadata(typeof(MyUserControl)));
    }

    public int Size
    {
        get { return (int)GetValue(SizeProperty); }
        set { SetValue(SizeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SizeProperty =
        DependencyProperty.Register("Size", typeof(int), typeof(MyUserControl), new UIPropertyMetadata(20));
}

参考链接

Another way is to declare a new dependency property and apply TemplateBinding

Here is the control template, where I set bind the Size property to the width.

<Style TargetType="{x:Type local:MyUserControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyUserControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox Width="{TemplateBinding Size}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>



public class MyUserControl : Control
{
    static MyUserControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyUserControl), new FrameworkPropertyMetadata(typeof(MyUserControl)));
    }

    public int Size
    {
        get { return (int)GetValue(SizeProperty); }
        set { SetValue(SizeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Size.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SizeProperty =
        DependencyProperty.Register("Size", typeof(int), typeof(MyUserControl), new UIPropertyMetadata(20));
}

Reference Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文