wpf IP 地址文本框用户控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22176932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IP address textbox user control
提问by user1531186
i'm trying to create a user control that acts as an IP address holder. Generally the control is composed of 4 TextBoxes that together has the full IP address. in the user control code behind there is a public property that holds the IP address of type IPAddress. I have been trying to expose this property so i could bind a property from my ViewModel to it.
我正在尝试创建一个充当 IP 地址持有者的用户控件。通常,控件由 4 个文本框组成,它们一起具有完整的 IP 地址。在后面的用户控制代码中有一个公共属性,用于保存 IPAddress 类型的 IP 地址。我一直在尝试公开此属性,以便我可以将 ViewModel 中的属性绑定到它。
here is the property from the user control i want to expose:
这是我想公开的用户控件的属性:
public IPAddress IPAddressObject
{
get
{
return new IPAddress(m_IPAddress);
}
set
{
m_IPAddress = value.GetAddressBytes();
NotifyPropertyChanged("Octet1");
NotifyPropertyChanged("Octet2");
NotifyPropertyChanged("Octet3");
NotifyPropertyChanged("Octet4");
}
}
its value gets updated correctly but i can't get the value into my ViewModel variable Using Binding. i know i need to use a dependency property in some way, but i don't know how to tie its value with my property.
它的值得到正确更新,但我无法使用绑定将值放入我的 ViewModel 变量中。我知道我需要以某种方式使用依赖属性,但我不知道如何将其值与我的属性联系起来。
thanks ahead :)
提前谢谢:)
采纳答案by AlSki
Its easier than that, you just need to use a MaskedInputTextEdit such as this one, http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox
它比这更容易,您只需要使用像这样的 MaskedInputTextEdit,http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox
or pick one of these. Where can I find a free masked TextBox in WPF?
或选择其中之一。我在哪里可以找到 WPF 中的免费蒙版 TextBox?
回答by user1531186
well i found the solution, the problem was that my VM didn't update correctly, apparently i had to add a specific metadata to my user control's DP that says it binds in TwoWay mode. as follows:
好吧,我找到了解决方案,问题是我的 VM 没有正确更新,显然我必须向我的用户控件的 DP 添加一个特定的元数据,说它以双向模式绑定。如下:
public static readonly DependencyProperty MyCustomProperty =DependencyProperty.Register("MyCustom", typeof(IPAddress), typeof(IPAddressTextBox), new FrameworkPropertyMetadata(IPAddress.Parse("0.0.0.0"), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public IPAddress MyCustom
{
get
{
return this.GetValue(MyCustomProperty) as IPAddress;
}
set
{
this.SetValue(MyCustomProperty, value);
// NotifyPropertyChanged("MyCustom");
}
}

