繁体   English   中英

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

[英]UserControl: how to convert dependency properties and bind to the result

我有一个XAML UserControl,是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}"/>

如您所见, 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
    }

暂无
暂无

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

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