0

嗨,我正在使用 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> 
4

1 回答 1

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中删除Properties区域,并用这个替换:

    #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
于 2013-05-23T14:32:12.933 回答