【发布时间】:2018-03-26 14:28:52
【问题描述】:
我的自定义控件中有DependencyProperty。我将它绑定到某个对象中的某个属性。当我的控件出现时,它会从源中获取值并将其应用于自身。但是当我在输入字段中输入内容时,源值不会改变。我做错了什么?
控制声明:
public class ScriptComponentField : Control
{
public enum Datatype
{
String,
Integer,
Real,
Boolean,
Enumerable,
Code
}
public string Header
{
get { return (String)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
public static readonly DependencyProperty HeaderProperty = DependencyProperty.RegisterAttached(nameof(Header), typeof(string), typeof(ScriptComponentField), new PropertyMetadata(""));
public object Value
{
get { return GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached(nameof(Value), typeof(object), typeof(ScriptComponentField), new PropertyMetadata(null));
public Datatype ValueType
{
get { return (Datatype)GetValue(ValueTypeProperty); }
set { SetValue(ValueTypeProperty, Value); }
}
public static readonly DependencyProperty ValueTypeProperty = DependencyProperty.RegisterAttached(nameof(ValueType), typeof(Datatype), typeof(ScriptComponentField), new PropertyMetadata(Datatype.String));
public List<string> EnumerableItems
{
get { return (List<string>)GetValue(EnumerableItemsProperty); }
set { SetValue(EnumerableItemsProperty, Value); }
}
public static readonly DependencyProperty EnumerableItemsProperty = DependencyProperty.RegisterAttached(nameof(EnumerableItems), typeof(List<string>), typeof(ScriptComponentField), new PropertyMetadata(null));
static ScriptComponentField()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ScriptComponentField), new FrameworkPropertyMetadata(typeof(ScriptComponentField)));
}
}
属性Value 具有object 类型只是因为它可以包含string、bool、int 等任何值。
控制模板:
<Style TargetType="{x:Type Controls:ScriptComponentField}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:ScriptComponentField}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" SharedSizeGroup="name"/>
<ColumnDefinition SharedSizeGroup="value"/>
</Grid.ColumnDefinitions>
<Label Content="{TemplateBinding Header}" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox x:Name="InputString" Visibility="Collapsed" Grid.Column="1" Text="{TemplateBinding Value}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" Margin="0,2"/>
<Advanced:IntegerUpDown x:Name="InputInteger" Visibility="Collapsed" Grid.Column="1" Value="{TemplateBinding Value}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Center" Margin="0,2"/>
<CheckBox x:Name="InputBoolean" Visibility="Collapsed" Grid.Column="1" IsChecked="{TemplateBinding Value}" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ValueType" Value="String">
<Setter TargetName="InputString" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="ValueType" Value="Integer">
<Setter TargetName="InputInteger" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="ValueType" Value="Boolean">
<Setter TargetName="InputBoolean" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
MaxB 从可用的有限信息中提供了一个很好的猜测;但你应该把这个问题归结为minimal reproducible example。通过这样做,您可能会自己找到它。如果没有,通过给出一个可重复的问题,其他人可以帮助你解决它。但不是这样的。
-
现在我提供了控件类和样式的完整描述。
标签: c# wpf data-binding