验证Listbox MVVM

时间:2012-03-21 11:23:15

标签: c# silverlight validation mvvm caliburn.micro

我正在使用Caliburn.Micro从我的ViewModel中的BindableCollection执行基于约定的列表框内容的绑定。

我想对它进行验证,这样当没有选择并且用户退出表单时,他们会在列表框周围找到漂亮的红线,并显示错误消息,他们必须选择一个值。到目前为止,Caliburn在文本框验证方面做得很好,但我似乎无法在列表框上工作!

另一方面,有没有办法强制从VM运行完整的验证?目前我只是通过检查值并更改它们来进行手动验证,因此它会触发Validator,这有点倒退..

以下是我的观点代码:

<UserControl x:Class="AddSessionDialogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

<toolkit:BusyIndicator IsBusy="{Binding IsBusy}" BusyContent="{Binding IsBusyStatusDisplay}">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="5,9" />
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="5" />
            </Style>
        </Grid.Resources>
        <TextBlock Text="Session Name" />
        <TextBox x:Name="SessionName" Grid.Column="1"  />
        <TextBlock Text="Scenario" Grid.Row="1" />
        <ListBox x:Name="Scenarios" Grid.Row="1" Grid.Column="1" DisplayMemberPath="Name" Margin="5" />
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="3" Margin="5">
            <Button x:Name="Cancel" Height="28" Margin="5" Content="Cancel" Padding="0,2" Width="75" />
            <Button x:Name="OK" Height="28" Margin="5" Content="OK" Padding="0,2" Width="100" />
        </StackPanel>
    </Grid>
</toolkit:BusyIndicator>

以下是我的viewmodel的(相关)代码:

    public BindableCollection<Scenario> Scenarios { get; set; }

    [Required(ErrorMessage = "You must select a scenario")]
    public Scenario SelectedScenario { get; set; }

    private string _sessionName;

    [Required(ErrorMessage="Session Name is invalid")]
    public string SessionName
    {
        get { return _sessionName; }
        set { _sessionName = value; NotifyOfPropertyChange(() => SessionName); }
    }

    public IEnumerable<IResult> OK()
    {
        if (SelectedScenario == null)
        {
            // This forces validation check, but doesnt show validation error on the view!
            SelectedScenario = new Scenario();
            SelectedScenario = null;
        }
        if (SessionName == null)
            SessionName = "";
        if (Validator.TryValidateObject(this, new ValidationContext(this), new List<ValidationResult>()))
        {
            IsBusy = true;
            IsBusyStatusDisplay = "Creating Session...";
            yield return new CreateSessionResult(SelectedScenario.Id, SessionName);
            IsBusy = false;
            yield return Close();
        }
    }

0 个答案:

没有答案