Silverlight绑定到自定义控件中的依赖属性
我有一个自定义控件创建的实例在我的视图中,在我的自定义控件(一个文本框)我有一个依赖项属性设置绑定到文本值,这一切正常工作和更新。不过,我现在想要将我的视图的依赖项属性绑定到我的控件的依赖项属性,但我无法将其完成。继承人我的意思Silverlight绑定到自定义控件中的依赖属性
控制选位
<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值目的地在我的视图中具有与我的自定义控件的TextValue相同的值?谢谢:)
PS喜灰
右倾哦,其实我已经整理,现在这个问题并为他人谋取利益,我会尝试,并记下我是如何做到无我的代码尽我所能在眼前我。
好吧,首先我的自定义控件,我没有正确使用依赖属性(哈哈)开始。因此,我们先纠正一下。
public partial class CustomControl : UserControl
{
public DependencyProperty FieldValueProperty { get; set; }
public CustomControlViewModel Context = new CustomControlViewModel();
public int FieldValue
{
get { return (int) GetValue(FieldValueProperty); }
set
{
SetValue(FieldValueProperty,value);
}
}
public CustomControl()
{
this.DataContext = Context;
FieldValueProperty = DependencyProperty.Register("FieldValue", typeof (int), typeof (CustomControl),
new PropertyMetadata(FieldValueChanged));
InitializeComponent();
}
void FieldValueChanged(Object sender, DependencyPropertyChangedEventArgs e)
{
Context.FieldValue = (int)e.NewValue;
}
}
public class CustomControlViewModel : BaseClass
{
private int _fieldValue;
public int FieldValue
{
get { return _fieldValue; }
set
{
_fieldValue = value;
RaisePropertyChanged("FieldValue");
}
}
}
不少有变化,认为它最好的办法就是看公众fieldValue方法多数民众赞成它使用的GetValue和的SetValue简单地为你所依赖的属性方法CustomControl的一部分,当您使用的SetValue它使用依赖项属性,当值更改时,依赖项属性会调用您指定它在DependencyProperty.Register元数据中调用的FieldValueChanged方法。处理被更改的FieldValue的这个方法然后在CustomControlViewModel中设置FieldValue,这个public int设置为我们访问的私有值来返回字段值,但是如您注意到的那样,也调用RaisePropertyChanged(“FieldValue”)方法。现在这个方法是从我的BaseClass继承的,它实现了这个方法,它基本上实现了INotifyPropertyChanged的东西。所以现在当我的值基本上改变一个事件被推动,现在我所需要做的就是在我的视图中处理包含此控件的事件。此位是在Constuctor
CustomControlInstance.Context.PropertyChanged += FieldValueChanged(Object Sender, PropertyChangedEventArgs e);
然后一个简单的位应该是一个方法只是
void FieldValueChanged(Object Sender, PropertyChangedEventsArgs e)
{
this.FieldValue = (int)e.NewValue;
}
那然后更新我为与fieldValue从控制角度的fieldValue方法,多数民众赞成在它得到了一些漏洞但其最好的我可以记住/解释给你,希望这有助于人们,我当然明白依赖属性现在好多了:)
我会接受这个答案作为接受的答案,当我回到电脑上明天
这种方法的工作,但不是我想要的更新与正确的方式做到这一点与依赖性属性,当我得到时间 – lee 2010-08-09 11:05:19
+1这是一个很好的问题,它显示了很多误解和陷阱。可能需要一些时间才能提供完整的答案。关于需求的问题,您正试图创建一个只允许输入数字的控件,并且必须限制为最小/范围?您无法在SDK或Silverlight Toolkit中找到控件,例如设计'NumericUpDown'控件的样式? – AnthonyWJones 2010-08-03 14:15:45
这是非常有限的版本,它奠定了与该字段的值的简单绑定的基础。更多的功能最终将被要求,但我也可能在做其他控制时遇到同样的问题,所以最好先解决这个问题。我相信什么即时丢失是一些财产提出的东西,我没有任何运气实施工作 – lee 2010-08-03 15:45:10