I have a XAML UserControl, a combination of a Label, a TextBox and a ComboBox. In the textbox I type a double value, which must be adjusted with a factor determined by my choice in the ComboBox. Always ! (In fact a unit conversion operation: meters to kilometers, feet to centimeters, etc.) That way, I will always have consistent SI units "inside" the program The logical place to do that would be inside the UserControl's code-behind. Therefor I define a Dep Prop InternalValue, which will contain the adjusted value of the textbox. I need to be able to bind to that DP.
I tried to do something like the code below, but that won't fly: I get the indicated compile error in TextChanged. How can I do what I want?
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;
}
And here is the 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>
And the control will be used like this:
<ip:UCInputField
CBSelectedItem="{Binding .....}"
CBUnit="{Binding ....}"
LBContent="Length"
InternalValue="{Binding ...}"
TBText="{Binding Path=Ucl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
As you can see TextChanged
is a static method. So it needs a reference to an instance of your User-Control if you want to change properties there. One of the method parameters is of the type DependencyObject
which is the object that owns the dependency-property that has been changed and can be casted into your User-Control.
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.