UserControl:如何转换依赖项属性并绑定到结果

时间:2018-12-16 10:48:39

标签: c# xaml user-controls

我有一个XAML用户控件,是Label,TextBox和ComboBox的组合。 在文本框中,键入一个双精度值,该值必须根据在组合框中的选择确定的系数进行调整。永远! (实际上是单位转换操作:米到公里,英尺到厘米等) 这样,我将始终在程序内部拥有一致的SI单位 这样做的逻辑位置将在UserControl的代码内部。 因此,我定义了一个Dep Prop InternalValue,它将包含文本框的调整后的值。 我需要能够绑定到该DP。
我尝试执行类似以下代码的操作,但操作不成功:我在TextChanged中收到指示的编译错误。 我该怎么办?

        public string  TBText
    {
        get { return (string)GetValue(TBTextProperty); }
        set { SetValue(TBTextProperty, value); }
    }

    public static readonly DependencyProperty TBTextProperty =
        DependencyProperty.Register("TBText", typeof(string), typeof(UCInputField), new PropertyMetadata( new PropertyChangedCallback(TextChanged)));

    private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        InternalValue = Convert.ToDouble(e.NewValue) * factor; // error: an object reference is required.

    }

    public double InternalValue
    {
        get { return (double)GetValue(InternalValueProperty); }
        set { SetValue(InternalValueProperty, value); }
    }

    public static DependencyProperty InternalValueProperty =
        DependencyProperty.Register("InternalValue", typeof(double), typeof(UCInputField));


    public DimPostfix CBSelectedItem
    {
        get { return (DimPostfix)GetValue(CBSelectedItemProperty); }
        set { SetValue(CBSelectedItemProperty, value); }
    }

    public static readonly DependencyProperty CBSelectedItemProperty =
        DependencyProperty.Register("CBSelectedItem", typeof(DimPostfix), typeof(UCInputField),new PropertyMetadata(new PropertyChangedCallback(UnitChanged)));

    static double factor;
    private static void UnitChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        factor = (e.NewValue as DimPostfix).Factor;
    }

这是UserControl:

    <UserControl
    x:Class="MyProgram.Views.UCInputField"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="45"
    d:DesignWidth="250"
    mc:Ignorable="d">

    <StackPanel
        x:Name="LayoutRoot"
        Orientation="Horizontal">
        <Label      
            Content="{Binding LBContent}"
            Style="{StaticResource LabelStyle1}" />
        <TextBox
            x:Name="TB"
            HorizontalAlignment="Stretch"
            Text="{Binding TBText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <ComboBox
            x:Name="CBBox"
            Width="70"
            DisplayMemberPath="Name"
            ItemsSource="{Binding CBUnit}"
            SelectedItem="{Binding CBSelectedItem}"/>
    </StackPanel>
    </UserControl>

该控件将像这样使用:

<ip:UCInputField
  CBSelectedItem="{Binding .....}"
  CBUnit="{Binding ....}"
  LBContent="Length"
  InternalValue="{Binding ...}"
  TBText="{Binding Path=Ucl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

1 个答案:

答案 0 :(得分:0)

您会看到TextChanged是静态方法。因此,如果要在其中更改属性,则需要引用用户控件的实例。 方法参数之一是DependencyObject类型,它是拥有已更改的依赖项属性的对象,可以转换为用户控件。

private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var myControl = (UCInputField)d; //This is the reference that is needed
        myControl.InternalValue = Convert.ToDouble(e.NewValue) * factor; //You can user factor since it is static
    }