具有许多控件的 WPF UserControl - 如何创建映射到许多控件的依赖属性?
我已经成功创建了一个带有 Depedency 属性的 UserControl,允许我绑定到 UserControl 中的单个 TextBox。但是,当我的 UserControl 中有许多控件并且只想绑定到单个属性(根据许多控件中的值构建)时,我不确定如何执行此操作?
UserControl 有 3 个文本框,分别表示年、月和日期,我想将其绑定到单个日期属性,到目前为止我已经得到了:
<UserControl x:Class="MyApp.DateControl"...>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Name="textbox_year" />
<TextBox Name="textbox_month" />
<TextBox Name="textbox_day" />
</StackPanel>
</StackPanel>
</UserControl>
我需要在后面的代码中添加什么才能使从三个文本框获取日期属性在另一个容器中使用我的控件可以绑定到日期。我意识到因为我的 UserControl 是我必须创建依赖属性的目标,但它看起来很复杂..
public partial class DateControl : UserControl
{
public DateControl()
{
InitializeComponent();
}
public DateTime Date
{
get
{
DateTime dt;
if (DateTime.TryParseExact(String.Format("{0}-{1}-{2}", this.textbox_year.Text, this.textbox_month.Text, this.textbox_day.Text), "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out dt))
return dt;
else
return DateTime.MinValue;
}
}
I have successfully created a UserControl with a Depedency property allowing me to bind to a single TextBox within my UserControl. However Im unsure how to go about doing this when I have many Controls within my UserControl and only want to bind to single Property (built from the values in the many controls)?
The UserControl has 3 textboxes for year, month and date I want to bind this to a single Date Property, so far I have got this:
<UserControl x:Class="MyApp.DateControl"...>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Name="textbox_year" />
<TextBox Name="textbox_month" />
<TextBox Name="textbox_day" />
</StackPanel>
</StackPanel>
</UserControl>
What do I need to add to the code behind to make the Date Property got from the three textboxes so in another container using my control can just bind to Date. I realise since my UserControl is the target I have to make a Dependency Property but it seems so complicated..
public partial class DateControl : UserControl
{
public DateControl()
{
InitializeComponent();
}
public DateTime Date
{
get
{
DateTime dt;
if (DateTime.TryParseExact(String.Format("{0}-{1}-{2}", this.textbox_year.Text, this.textbox_month.Text, this.textbox_day.Text), "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out dt))
return dt;
else
return DateTime.MinValue;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用转换器来实现你想要的。
您的用户控件的 XAML 将如下所示:
在代码隐藏中,您将只有您的依赖属性:
并且转换器将如下所示:
I suggest using a converter to achieve what you want.
Your user control's XAML will look like this:
In code-behind you will have only you dependency property:
And the converter will look something like this:
这就是我个人处理类似事情的方式。通常,我会将成员移到一个单独的逻辑类中,并在设置器中包含一些其他验证。
还有xaml
This is how I personally would approach something like this. Normally, I would move the members out into a separate logic class and include some other validation in the setters.
And the xaml
如果您提到的情况是针对 DateTime,那么您可以选择 Masked TextBox。
WPF 屏蔽文本框,其值不包含掩码
If the case you mentioned is for DateTime, then you can go for Masked TextBox.
WPF Masked Textbox with a value that does not contain mask
您可以使用 TextBoxes 上的 TextChanged 事件来设置日期:
XAML:
和容器:
当然,这非常简单。剩下的留给读者作为练习:)
You can use the TextChanged events on the TextBoxes to set the date:
XAML:
And the container:
It's very simplistic of course. The rest is left as an exercise for the reader :)