usercontrol中的MVVM文本框绑定到不同的类型

时间:2012-05-29 14:54:01

标签: mvvm user-controls

我在网格中使用usercontrol。此usercontrol包含一个文本框,该文本框绑定了viewmodel提供的对象的属性。我的问题是属性可以是字符串或int。

我将依赖项属性设置为字符串,因此它可以正常显示,但如果用户将字符串输入到int字段中,则视图模型不会抛出验证错误,但当然我无法保存到数据库

usercontrol XAML

<UserControl x:Class="GenericFileTransferClient.Views.UserControlTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="GridLayout">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="7*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="{Binding Path=LabelText}" VerticalAlignment="Center" Style="{StaticResource ResourceKey=TextBlockWrapping}" />
        <DockPanel Grid.Column="1">
            <Border DockPanel.Dock="Right"
                                ToolTip="" Style="{StaticResource BorderReportDetail}">
                <TextBlock Text="?" Style="{StaticResource TextBoxReportDetail}"></TextBlock>
            </Border>
            <TextBox Text="{Binding Path=TextBoxText}"/>
        </DockPanel>
    </Grid>
</UserControl>

后面的用户控制代码

public partial class UserControlTextBox : UserControl
{
    public static DependencyProperty LabelTextProperty = DependencyProperty.Register("LabelText", typeof(String), typeof(UserControl));
    public static DependencyProperty TextBoxTextProperty = DependencyProperty.Register("TextBoxText", typeof(String), typeof(UserControl));


    public String LabelText
    {
        get { return (String)GetValue(LabelTextProperty); }
        set { SetValue(LabelTextProperty, value); }
    }

    public String TextBoxText
    {
        get { return (String)GetValue(TextBoxTextProperty); }
        set { SetValue(TextBoxTextProperty, value); }
    }

    public UserControlTextBox()
    {
        InitializeComponent();
        GridLayout.DataContext = this;
    }
    }

在主网格中调用usercontrol的示例

<views:UserControlTextBox LabelText="Report Name:" TextBoxText="{Binding Path=CurrentReport.ReportName}" Grid.Row="1" Grid.ColumnSpan="2"/>

            <TextBlock Text="Header:" Grid.Row="2"/>
            <CheckBox Grid.Column="1" Grid.Row="2" IsChecked="{Binding Path=CurrentReport.Header}" 
                      VerticalContentAlignment="Stretch" VerticalAlignment="Center" Name="HeaderCheckBox"/>

            <Grid Grid.Row="3" Grid.ColumnSpan="2" Visibility="{Binding IsChecked, ElementName=HeaderCheckBox, Converter={StaticResource BoolToVisConverter}}">

                <views:UserControlTextBox LabelText="# Row Header:" TextBoxText="{Binding Path=CurrentReport.HeaderRow}"/>
            </Grid>

如您所见,CurrentReport.ReportName是一个字符串,但CurrentReport.HeaderRow是一个int。

是否有任何方法可以使依赖项属性通用或基于传递给usercontrol的参数。或者有没有办法在用户点击保存按钮之前进行验证?

谢谢

2 个答案:

答案 0 :(得分:1)

我会编写一个简单的IntToStringConverter,然后在TextBoxText绑定中使用它将字符串传递给您的usercontrol。

答案 1 :(得分:0)

我在这里写了很多时间。这种情况最简单的方法是在viewmodel中使用字符串Property来绑定到您的视图。那么你只需要在你的IDataErrorInfo中验证这个属性为int。如果这个成功,你可以在调用save命令之前将你的字符串转换为int。

只要你的viewmodel中有一个int属性,你就可以在输入不能转换为int时始终收到BindingException。

ps:确保这样做的一种方法是使用数字文本框或仅允许数字的屏蔽行为。

相关问题