您好我正在使用MVVM模型,我跟着 http://firstbit.blogspot.com/2011/07/wpf-c-ipaddress-control.html 用于IP控制。 但是当我应用绑定时它没有显示在控件中。 任何人都可以帮助我。
cs:IPAddressControl Text="{Binding ViewData.StartIP,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding}" x:Name="startIPControl" Grid.Row="3" Grid.Column="1" VerticalContentAlignment="Center" OverridesDefaultStyle="False">
</cs:IPAddressControl>
答案 0 :(得分:0)
从它的外观来看,控件没有为Text属性实现依赖属性,所以可能这就是问题所在。
据我所知,你只能绑定到DependencyObject上的DependencyProperty,看看这里:
http://www.codeproject.com/Articles/71348/Binding-on-a-Property-which-is-not-a-DependencyPro
我没有时间从您提供的链接向您发送控件的更新版本,但我可以告诉我我对这个也不支持绑定做了什么:
https://wpfipaddress.codeplex.com/
在 IPAddressControl.xaml.cs 中下载其来源,删除属性区域,并替换为此区域:
#region Text dependency property
/// <summary>
/// Dependency property field for TextProperty.
/// </summary>
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(IPAddressControl), new PropertyMetadata("0.0.0.0", new PropertyChangedCallback(OnTextPropertyChanged)));
#region Text dependency property callbacks
/// <summary>
/// Dependency property TextProperty changed callback.
/// </summary>
/// <param name="d">Dependency object.</param>
/// <param name="e">Event arguments.</param>
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
IPAddressControl o = d as IPAddressControl;
// Do stuff here, then raise event for property changed.
try
{
var value = e.NewValue as string;
var splitValues = new string[4];
if (value != null)
{
var splits = value.Split(new char[] { '.' }, StringSplitOptions.None);
Array.Copy(splits, splitValues, splits.Length);
}
o.txtboxFirstPart.Text = splitValues[0];
o.txtboxSecondPart.Text = splitValues[1];
o.txtboxThridPart.Text = splitValues[2];
o.txtboxFourthPart.Text = splitValues[3];
}
catch (Exception ex)
{
throw new Exception("Error in IP control see inner exception!", ex);
}
o.RaiseTextChanged(e);
}
#endregion Text dependency property callbacks
/// <summary>
/// Gets or sets Text.
/// </summary>
public string Text
{
get { return (string)this.GetValue(IPAddressControl.TextProperty); }
set { this.SetValue(IPAddressControl.TextProperty, value); }
}
/// <summary>
/// Occurs when dependency property TextProperty changed.
/// </summary>
public event DependencyPropertyChangedEventHandler TextChanged;
/// <summary>
/// Raises TextChanged event.
/// </summary>
/// <param name="e">Event arguments.</param>
protected virtual void RaiseTextChanged(DependencyPropertyChangedEventArgs e)
{
if (this.TextChanged != null)
this.TextChanged(this, e);
}
#endregion Text dependency property