.net WPF MVVM+;带代码隐藏的用户控件

.net WPF MVVM+;带代码隐藏的用户控件,.net,wpf,mvvm,user-controls,.net,Wpf,Mvvm,User Controls,由于某些原因,我在MVVM WPF应用程序中通过ViewModel绑定自定义用户控件时遇到问题。基本控件是一个带有三个文本框的日期输入表单。我正在使用usercontrol的代码隐藏来捕获textchange事件和一些操作。由于某些原因,向属性添加绑定不会触发 用户控件的XAML: <UserControl x:Class="MYLibray.DateBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

由于某些原因,我在MVVM WPF应用程序中通过ViewModel绑定自定义用户控件时遇到问题。基本控件是一个带有三个文本框的日期输入表单。我正在使用usercontrol的代码隐藏来捕获textchange事件和一些操作。由于某些原因,向属性添加绑定不会触发

用户控件的XAML:

<UserControl x:Class="MYLibray.DateBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="800">
<StackPanel>

    <Border CornerRadius="10" Height="200" BorderBrush="Gray" Background="Gray">
    <StackPanel Orientation="Horizontal" OpacityMask="{x:Null}" HorizontalAlignment="Center">
    <TextBox Name="txtMonth" Height="100" Width="90" BorderThickness="0,0,0,5" Background="{x:Null}" Text="" FontSize="72" TextChanged="TextChanged">
        <TextBox.BorderBrush>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF000000" Offset="0"/>
                <GradientStop Color="#FF000000" Offset="1"/>
            </LinearGradientBrush>
        </TextBox.BorderBrush>
    </TextBox>
            <TextBlock Text="/" FontSize="72" Height="100" Width="50" />
            <TextBox x:Name="txtDay" Height="100" Width="90" Background="{x:Null}" BorderThickness="0,0,0,5" VerticalAlignment="Stretch" FontSize="72" TextChanged="TextChanged">
                <TextBox.BorderBrush>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF000000" Offset="0"/>
                        <GradientStop Color="#FF000000" Offset="1"/>
                    </LinearGradientBrush>
                </TextBox.BorderBrush>
            </TextBox>
            <TextBlock Text="/19" FontSize="72" Height="100" Width="Auto" />
            <TextBox x:Name="txtYear" Height="100" Width="90" Background="{x:Null}" BorderThickness="0,0,0,5"  FontSize="72" TextChanged="TextChanged">
                <TextBox.BorderBrush>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF000000" Offset="0"/>
                        <GradientStop Color="#FF000000" Offset="1"/>
                    </LinearGradientBrush>
                </TextBox.BorderBrush>
            </TextBox>
    </StackPanel>
    </Border>

</StackPanel>
{

私有字符串_date=”“;
public static dependencProperty TextProperty=dependencProperty.RegisterAttached(“DateText”、typeof(string)、typeof(DateBox)、new PropertyMetadata(TextPropertyChanged));
public static dependencProperty EnabledProperty=dependencProperty.RegisterAttached(“Enabled”,typeof(bool),typeof(DateBox),null);
公共日期框()
{                                 
初始化组件();
this.DataContext=this;
txtmount.Focus();
}
公共布尔启用
{
得到
{
返回(bool)GetValue(DateBox.EnabledProperty);
}
设置
{
设置值(DateBox.EnabledProperty,值);
txtDay.IsEnabled=值;
txtMonth.IsEnabled=值;
txtYear.IsEnabled=值;
}
}
公共字符串日期文本
{
得到
{
返回(字符串)this.GetValue(TextProperty);
}
设置
{
this.SetValue(TextProperty,value);
}
}
静态无效TextPropertyChanged(DependencyObject属性,DependencyPropertyChangedEventArgs参数)
{
((日期框)属性).OnTextPropertyChanged((对象)args.NewValue);
}
私有void OnTextPropertyChanged(对象newValue)
{
_date=newValue.ToString();
this.UpdateLayout();
日期时间;
if(DateTime.TryParse(_date,out d))
{
txtDay.Text=d.Day.ToString();
txtmount.Text=d.Month.ToString();
Text=(d.Year-1900).ToString();
}
其他的
{
txtDay.Text=“”;
txtmount.Text=“”;
txtYear.Text=“”;
}
DateText=d.ToSortDateString();
}
布尔区域有效数字字符(字符串str)
{
bool-ret=真;
if(str==System.Globalization.NumberFormatInfo.CurrentInfo.PositiveDesign)
{
返回ret;
}
int l=结构长度;
对于(int i=0;i1)
{
字符串值=txt.Text;
txt.Text=“0”+值;
txtDay.Focus();
}
}
如果(txt.Text.Length==2)
{
txtDay.Focus();
}
打破
案例“txtDay”:
如果(txt.Text.Length==1)
{
if(Convert.ToInt32(txt.Text)>3)
{
字符串值=txt.Text;
txt.Text=“0”+值;
txtwear.Focus();
}
}
如果(txt.Text.Length==2)
{
txtwear.Focus();
}
打破
案例“TXT年”:
如果(txt.Text.Length==2)
{
日期时间;
字符串datestring=txtmount.Text+“/”+txtDay.Text+“/19”+txtdyear.Text;
if(DateTime.TryParse(datestring,out d))
{
DateText=d.ToSortDateString();
}
}
打破
违约:
打破
}
}
}

当我在DataTemplate中创建usercontrol时,如下所示:

<uc:DateBox DateText="{Binding BirthDate}" />


my ViewModel中生日的获取和设置从未设置。VM上的生日是一个字符串。

检查控件和父控件的
DataContext

要帮助调试绑定,请签出

基本上,将这个
xmlns
添加到控件中

xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
然后将其添加到您的
绑定中


检查控件和父控件的
DataContext

要帮助调试绑定,请签出

基本上,将这个
xmlns
添加到控件中

xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
然后将其添加到您的
绑定中


非常感谢。UserControl的DataContext是我的问题。谢谢。UserControl的DataContext是我的问题。
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
{Binding ....    , diagnostics:PresentationTraceSources.TraceLevel=High }