如何在wpf中绑定来自userControl的边距或厚度

时间:2016-07-10 17:23:03

标签: c# wpf binding dependency-properties multibinding

我搜索了许多时间和次数,并且秃顶了 所以无法找到完整的简单示例。

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

请给我简单而完整的例子。 非常感谢!

1 个答案:

答案 0 :(得分:0)

这需要几个步骤: 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控件并按以下方式绑定它们:
  2. <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属性):
    2. <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]));
          }
      }
      

      希望它有所帮助!