【发布时间】:2011-03-24 16:26:17
【问题描述】:
我已经创建了一个自定义控件,它在我的视图中被实例化,在我的自定义控件(文本框)中,我设置了一个绑定到文本值的依赖属性,这一切都可以正常工作并更新。但是,我现在想将我的视图的依赖属性绑定到我的控件的依赖属性,但我无法让它完全正常工作。这就是我的意思
控制选定的位
<TextBox x:Name="txtBox"
GotFocus="txtBox_GotFocus"
LostFocus="txtBox_LostFocus"
Grid.Row="0"
Text="{Binding TextValueProperty, Mode=TwoWay}"/>
public DependencyProperty TextValueProperty{ get; set; }
public int TextValue
{
get { return (int)GetValue(TextValueProperty); }
set
{
if ((value >= MinValue) && (value <= MaxValue))
{
SetValue(TextValueProperty, value);
}
}
}
public CustomControl()
{
TextValueProperty = DependencyProperty.Register("TextValueProperty ", typeof(int), typeof(CustomControl), new PropertyMetadata(null ));
}
查看选定的位
<my:CustomControl x:Name="TxtBoxInt" Grid.RowSpan="2" Grid.Row="0" Grid.Column="1"/>
我想这样做:
<my:CustomControlx:Name="TxtBoxInt" TextValueProperty="{Binding DestinationProperty}" Grid.RowSpan="2" Grid.Row="0" Grid.Column="1"/>
和代码视图
public DependencyProperty DestionationProperty;
public int Destination
{
get
{
return (int)GetValue(DestionationProperty);
}
set
{
SetValue(DestionationProperty, value);
}
}
public CustomControl()
{
DestionationProperty = DependencyProperty.Register("DestionationProperty", typeof(int), typeof(CustomControl), null);
InitializeComponent();
}
那么我需要做些什么才能将我的 int 值 Destination 在我的视图中绑定为与我的自定义控件中的 TextValue 具有相同的值?谢谢:)
ps嗨灰
【问题讨论】:
-
+1 这是一个很好的问题,因为它一次性展示了许多误解和陷阱。提供完整的答案可能需要一些时间。关于要求的问题,您正在尝试创建一个仅允许输入数字并且必须限制在最小值/范围内的控件?在 SDK 或 Silverlight Toolkit 中找不到控件,例如设置
NumericUpDown控件的样式? -
这是非常有限的版本,它奠定了与字段值进行简单绑定的基础。最终将需要更多功能,但我在进行其他控制时也可能会遇到同样的问题,所以最好先解决这个问题。我相信我缺少的是一些财产筹集的东西,我没有任何运气来实施
标签: c# silverlight data-binding binding dependency-properties