[英]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.