Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf Silverlight用户控件中的自定义绑定_Wpf_Silverlight_Mvvm_User Controls - Fatal编程技术网

Wpf Silverlight用户控件中的自定义绑定

Wpf Silverlight用户控件中的自定义绑定,wpf,silverlight,mvvm,user-controls,Wpf,Silverlight,Mvvm,User Controls,我在MVVM Silverlight应用程序中通过ViewModel绑定自定义用户控件时遇到问题。基本控件是一个带有三个文本框的日期输入表单。我正在使用MVVM获取三个属性中的文本框数据,然后对其进行验证 将下面的控件绑定到MVVM对我来说很好: 用户控件的XAML:DateControl.XAML <UserControl x:Class="DatePicker.DateControl" xmlns="http://schemas.microsoft.com/winfx/2006

我在MVVM Silverlight应用程序中通过ViewModel绑定自定义用户控件时遇到问题。基本控件是一个带有三个文本框的日期输入表单。我正在使用MVVM获取三个属性中的文本框数据,然后对其进行验证

将下面的控件绑定到MVVM对我来说很好:

用户控件的XAML:DateControl.XAML

<UserControl x:Class="DatePicker.DateControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="LayoutRoot" Background="White"
          HorizontalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Horizontal">
                <TextBox MaxLength="2" TabIndex="0" Style="{StaticResource BirthDDTextBoxStyle}"
                                Text="{Binding BirthDay,Mode=TwoWay,ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
                <TextBlock  Style="{StaticResource TextBlockStyle_DateSeperator}"/>
                <TextBox MaxLength="2" TabIndex="1" Style="{StaticResource BirthDDTextBoxStyle}" Text="{Binding BirthMonth, Mode=TwoWay,ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
                <TextBlock   Style="{StaticResource TextBlockStyle_DateSeperator}"/>
                <TextBox MaxLength="4" TabIndex="2" Style="{StaticResource BirthYYTextBoxStyle}" Text="{Binding BirthYear, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
                <Button Content="Show" Width="100" Height="30" Click="Button_Click"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

日期控制视图模型

<UserControl x:Class="DatePicker.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DatePicker"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <local:DateControl />
    </Grid>
</UserControl>
名称空间日期选择器
{
公共类DatePickServiceWModel:EntityViewModel
{
公共DatePickerViewModel()
{
}
私人日期时间出生日期;
公共日期时间出生日期
{
得到
{
返回出生日期;
}
设置
{
出生日期=价值;
}
}
私人串生日;
公共字符串生日
{
获得{返回生日;}
设置
{
生日=价值;
房地产商(“生日”);
确认生日(“生日”,价值);
}
}
私人串生日月;
公共字符串生日月
{
获取{返回出生月份;}
设置
{
出生月份=数值;
财产变更处理人(“生日”);
验证月(“生日月”,值);
}
}
私弦生日;
公共字符串生日
{
获取{返回出生年份;}
设置
{
生日=价值;
房地产商(“生日”);
验证出生年份(“出生年份”,值);
}
}
私有void ValidateDOB()
{
ClearErrorFromProperty(“生日”);
ClearErrorFromProperty(“生日月”);
ClearErrorFromProperty(“生日”);
if(string.IsNullOrEmpty(生日))
确认生日(“生日”,生日);
if(string.IsNullOrEmpty(BirthMonth))
月(“生日月”,生日月);
if(string.IsNullOrEmpty(生日))
出生年份(“出生年份”,出生年份);
如果(!string.IsNullOrEmpty(生日)和&!string.IsNullOrEmpty(生日月)和&!string.IsNullOrEmpty(生日年))
SetDateOfBirth();
}
私有void validateBithDay(字符串属性名称,字符串值)
{
ClearErrorFromProperty(propertyName);
if(String.IsNullOrEmpty(value))
AddErrorProperty(propertyName,“Resources.PatientImport.EmptyDayMessage”);
其他的
if(公共IsNumber(值))
{
整数天=转换为32(值);
如果(天>31 | |天<1)
AddErrorProperty(propertyName,“Resources.PatientImport.InvalidDayMessage”);
}
其他的
AddErrorProperty(propertyName,“Resources.PatientImport.InvalidDayMessage”);
}
私有void validateBithMonth(字符串属性名称,字符串值)
{
ClearErrorFromProperty(propertyName);
if(String.IsNullOrEmpty(value))
AddErrorProperty(propertyName,“Resources.PatientImport.EmptyMonthMessage”);
其他的
if(公共IsNumber(值))
{
整月=转换为32(值);
如果(月>12 | |月<1)
AddErrorProperty(propertyName,“Resources.PatientImport.InvalidMonthMessage”);
}
其他的
AddErrorProperty(propertyName,“Resources.PatientImport.InvalidMonthMessage”);
}
私有void validateBithYear(字符串属性名称,字符串值)
{
ClearErrorFromProperty(propertyName);
if(String.IsNullOrEmpty(value))
AddErrorProperty(propertyName,“Resources.PatientImport.EmptyYearMessage”);
其他的
if(公共IsNumber(值))
{
int year=转换为NT32(值);
如果(year.ToString().Length>4 | | year.ToString().Length<4)
AddErrorProperty(propertyName,“Resources.PatientImport.InvalidYearMessage”);
}
其他的
AddErrorProperty(propertyName,“Resources.PatientImport.InvalidYearMessage”);
}
私有void SetDateOfBirth()
{
字符串dateString=生日+“-”+生日+“-”+生日;
日期时间日期;
如果(!DateTime.TryParse(dateString.ToString(),out date))
{
ClearErrorFromProperty(“生日”);
ClearErrorFromProperty(“生日月”);
ClearErrorFromProperty(“生日”);
AddErrorProperty(“生日”、“Resources.PatientImport.InvalidDayMessage”);
AddErrorProperty(“生日月”、“资源.病人报告.无效月消息”);
AddErrorProperty(“生日”、“资源.患者报告.无效消息”);
}
其他的
生日=日期;
}
}
}
我想在我的主页中使用此控件。xaml

<UserControl x:Class="DatePicker.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DatePicker"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <local:DateControl />
    </Grid>
</UserControl>

我想在DateControl中定义一个属性,用于将我的UserControl与MainPage.xaml的ViewModel绑定

<UserControl x:Class="DatePicker.DateControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="LayoutRoot" Background="White"
          HorizontalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Horizontal">
                <TextBox MaxLength="2" TabIndex="0" Style="{StaticResource BirthDDTextBoxStyle}"
                                Text="{Binding BirthDay,Mode=TwoWay,ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
                <TextBlock  Style="{StaticResource TextBlockStyle_DateSeperator}"/>
                <TextBox MaxLength="2" TabIndex="1" Style="{StaticResource BirthDDTextBoxStyle}" Text="{Binding BirthMonth, Mode=TwoWay,ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
                <TextBlock   Style="{StaticResource TextBlockStyle_DateSeperator}"/>
                <TextBox MaxLength="4" TabIndex="2" Style="{StaticResource BirthYYTextBoxStyle}" Text="{Binding BirthYear, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
                <Button Content="Show" Width="100" Height="30" Click="Button_Click"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>
比如:

<Grid x:Name="LayoutRoot" Background="White">
    <local:DateControl Date="{Binding BirthDate, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"/>
</Grid>

生日是字符串类型属性。

如何更改usercontrol以便绑定