WPF Tabcontrol。具有CanExecute条件的命令的绑定按钮位于子视图中

时间:2016-05-26 15:38:01

标签: c# wpf button command tabcontrol

这就是场景:在UserControl中有TabControl,它加载不同的视图和一个按钮。喜欢这张图片:

Scenario

如果字段“名称”和“所有者”不为空,则只能启用按钮“保存”。 这些字段位于ItemTab中加载的子视图中。

这是XAML(只有1个TabItem才能简化)

<UserControl 
    x:Class="Winvet.Desktop.Views.VCliente.DatosCliente"
    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" 
    xmlns:viewModels="clr-namespace:Winvet.Desktop.ViewModels.VMCliente"
    xmlns:views="clr-namespace:Winvet.Desktop.Views.VCliente">

    <Grid Margin="10 5 10 10">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="7*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>

        <TabControl Grid.Column="0" Name="TabDatosCliente" ItemsSource="{Binding ItemsTabDatosCliente}" SelectedIndex="0">

            <TabControl.Resources>
                <DataTemplate DataType="{x:Type viewModels:DatosClienteGeneralViewModel}">
                    <views:DatosClienteGeneral/>
                </DataTemplate>
            </TabControl.Resources>

            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}" />
                </DataTemplate>
            </TabControl.ItemTemplate>  

        </TabControl>

        <StackPanel Grid.Column="1" Orientation="Horizontal">
            <Button VerticalAlignment="Bottom" Command="{ I want to bind this }">Guardar</Button>
        </StackPanel>

    </Grid>
</UserControl>

这是ViewModel(只有1个TabItem来简化)

using System.Collections.ObjectModel;
using Winvet.Desktop.Common;

namespace Winvet.Desktop.ViewModels.VMCliente
{
    public class DatosClienteViewModel: ViewModelBase
    {                 
        public ObservableCollection<ViewModelBase> ItemsTabDatosCliente { get; private set; }
        public DatosClienteViewModel()
        {
            ItemsTabDatosCliente = new ObservableCollection<ViewModelBase>
            {
                new DatosClienteGeneralViewModel()
            };
        }
    }
}

我不想创建一个Command来检查这两个子视图字段是否为空并启用按钮。我该怎么办?

1 个答案:

答案 0 :(得分:0)

路由命令会在整个界面中挖掘和冒泡,因此只要您与引发事件的项目位于同一个可视分支中,就可以在任何地方处理它。

所以在你的视图中

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        CommandBindings.Add(new CommandBinding(command, execute, canExecute));
    }

    private void canExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        throw new NotImplementedException();
        //at this point you can pass it to your ViewModel
    }

    private void execute(object sender, ExecutedRoutedEventArgs e)
    {
        throw new NotImplementedException();
        //at this point you can pass it to your ViewModel
    }
}

其中command是在按钮Command Property

上设置的RoutedCommand