繁体   English   中英

如何在 wpf 中从 userControl 绑定边距或厚度

[英]How bind Margin or thickness from userControl in wpf

我搜索了很多次,秃了头,所以找不到完整的简单例子。

我在 WPF 中创建了一个用户控件,它有四个文本框(右、左、上、下标题)。 当我在窗口中使用该用户控件时,我只是想将这些值绑定到某些元素的厚度或边距,例如矩形的笔划厚度。

请给我简单而完整的例子。 多谢!

这将需要几个步骤: 1. 在用户控件中创建 4 个依赖项属性(出于示例的目的,我将它们命名为 Text1 到 Text4):

public partial class MyUserControl : UserControl
{
    public string Text1
    {
        get { return (string)GetValue(Text1Property); }
        set { SetValue(Text1Property, value); }
    }

    public static readonly DependencyProperty Text1Property = DependencyProperty.Register("Text1", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));

    public string Text2
    {
        get { return (string)GetValue(Text2Property); }
        set { SetValue(Text2Property, value); }
    }

    public static readonly DependencyProperty Text2Property = DependencyProperty.Register("Text2", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));

    public string Text3
    {
        get { return (string)GetValue(Text3Property); }
        set { SetValue(Text3Property, value); }
    }

    public static readonly DependencyProperty Text3Property = DependencyProperty.Register("Text3", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));

    public string Text4
    {
        get { return (string)GetValue(Text4Property); }
        set { SetValue(Text4Property, value); }
    }

    public static readonly DependencyProperty Text4Property = DependencyProperty.Register("Text4", typeof(string), typeof(MyUserControl), new PropertyMetadata(string.Empty));

    public MyUserControl()
    {
        InitializeComponent();
    }
}
  1. 接下来,在 UserControl 的 XAML 中添加 4 个 TextBox 控件并按以下方式绑定它们:
<TextBox Text="{Binding Text1, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
    <TextBox Text="{Binding Text2, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
    <TextBox Text="{Binding Text3, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
    <TextBox Text="{Binding Text4, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
  1. 将 UserControl 放在您要使用它的任何窗口中,并将 Text1 到 Text4 属性绑定到窗口中的其他控件和属性(例如,我将属性连接到窗口的 Title 属性):
<local:MyUserControl Text1="{Binding Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
    Text2="{Binding Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
    Text3="{Binding Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
    Text4="{Binding Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />

更新:一个简单的粗细到字符串转换器,可用于绑定到诸如 Margin 之类的属性:

public class ThicknessToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Thickness thikness = (Thickness)value;

        return $"{thikness.Left},{thikness.Top},{thikness.Right},{thikness.Bottom}";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Because you are not using a view/view model, validation should go here!
        string[] thicknessValues = ((string)value).Split(',');

        return new Thickness(double.Parse(thicknessValues[0]),
            double.Parse(thicknessValues[1]),
            double.Parse(thicknessValues[2]),
            double.Parse(thicknessValues[3]));
    }
}

希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM