如果不满足条件,则避免执行从Button_Click事件绑定到按钮的命令

时间:2012-04-03 15:26:27

标签: wpf mvvm tabcontrol

我有下一个场景:带有tabControl和按钮的视图:

<Window x:Class="some"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="Settings" Height="412" Width="586"
    DataContext="{Binding SettingsViewModel}">
<Grid>
    ...
    <TabControl Name="TabControlSettings" Grid.Row="0" Grid.ColumnSpan="3" Margin="10, 10, 10, 10">            
        <TabItem Header="settings1">                
        ...
        </TabItem>
        <TabItem Header="settings1">
        ...
        </TabItem>
        <TabItem Header="settings2">
         ...
        </TabItem>
        <TabItem Header="settings3">
        ...
        </TabItem>
        <TabItem Header="settings3">
        ...            
        </TabItem>
    </TabControl>
    <Button Grid.Row="1" Grid.Column="2" Name="btn_Ok" Content="Ok" HorizontalAlignment="Left" Width="50" Height="23" Margin="10"  Command="{Binding SaveSettingsCommand}" Click="btn_Ok_Click" />       

</Grid>

在后面的代码中定义的btn_Ok_Click事件中捕获了一些UI验证:

private void btn_Ok_Click(object sender, RoutedEventArgs e)
    {
        bool HasErrors = false;

        HasErrors = !ViewServices.IsValid(TabControlSettings);
        if (HasErrors)
        {
            var control = ViewServices.controlToValidate.Values.First() as System.Windows.Controls.Control;
            control.Focus();
        }
        else
        { 
            this.Close();
        }            
    }

ViewServices只是一个帮助类,它为视图提供了一些方法,就像IsValid一样:

 public static Dictionary<bool, object> controlToValidate;


    /// <summary>
    /// validates all binding rules on a dependency object and its children:
    /// </summary>
    /// <param name="obj">Dependency object</param>
    /// <returns>True if none of the child objects has errors, False otherwise</returns>
    internal static bool IsValid(DependencyObject parent) 
    {
        controlToValidate = new Dictionary<bool, object>();
        // The dependency object is valid if it has no errors,
        //and all of its children (that are dependency objects) are error-free.
        if (Validation.GetHasError(parent))
        {
            var obj = GetTabItem(parent);
            controlToValidate.Add(false, obj);             
            return false;
        }
        foreach (DependencyObject child in LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>())
        {
            if (!IsValid(child))
            {
                if (controlToValidate.Count == 0)
                {
                    var obj = GetTabItem(child);
                    controlToValidate.Add(false, obj);                        
                }
                return false;
            }
        }
        return true;            
    } 

    internal static object GetTabItem(DependencyObject obj)
    {
        var parent = LogicalTreeHelper.GetParent(obj);
        var parentControl = parent as System.Windows.Controls.Control;
        if (parentControl != null)
        {
            if (parentControl.GetType() != typeof(System.Windows.Controls.TabItem))
            {
                return GetTabItem(parent);
            }
            else
                return parentControl;
        }
        else
        {
            return GetTabItem(parent);
        }               
    }

使用这种方法,我得到包含有验证错误的控件的tabitem。

现在再次返回后面代码中的btn_OK_Click事件:

HasErrors = !ViewServices.IsValid(TabControlSettings);
        if (HasErrors)
        {
            var control = ViewServices.controlToValidate.Values.First() as System.Windows.Controls.Control;
            control.Focus();
        }
        else
        { 
            this.Close();
        }

我注意到在绑定到按钮的命令之前调用了按钮的Click事件(我是对的吗?)。

如果HasErrors为true,我想避免执行SaveSettingsCommand。怎么办呢?

1 个答案:

答案 0 :(得分:0)

也许它可以帮助您了解当HasErrors等于true时如何使用Validations禁用按钮。

http://www.wpfsharp.com/2012/02/03/how-to-disable-a-button-on-textbox-validationerrors-in-wpf/