CanExecute命令执行更新而不是执行命令

时间:2013-07-20 22:49:27

标签: wpf commandbinding

我在C#visual studio 2010中创建了一些方法,使用wpf命令禁用或启用记录中的按钮,如下所示:

     private void CanSavePageCommandExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        **if (studentsViewSource != null && studentsViewSource.View != null && novelDataSetTableAdapter.Update(novelDataSet.students) > 0)
        {
            e.CanExecute = true;
            //e.Handled = true;
        }
        else
        {
            e.CanExecute = false;
        }
    }**

应该是:

  private void CanSavePageCommandExecute(object sender, CanExecuteRoutedEventArgs e)
    {

       e.CanExecute = studentsViewSource != null && this.novelDataSet.HasChanges();
       e.Handled = true;

    }

     private void OnSavePageCommandExecute(object sender, ExecutedRoutedEventArgs e)
    {
        if (novelDataSetTableAdapter.Update(novelDataSet.students) > 0)

            MessageBox.Show("Entries have been updated !", "DATA STATUS");
        else
            MessageBox.Show("Error occurred!", "DATA NOT SAVED");
    }

' CanSavePageCommandExecute'如果满足所附条件,则应该禁用SAVE按钮。它实际上是这样做的,但它更进一步,并将数据保存到数据库。 ' OnSavePageCommandExecute'即使我只使用MessageBox.show测试,也不会执行命令。 如何根据对数据集所做的更改,阻止CanExecute命令将数据保存为仅启用和禁用按钮?

我没有使用MVVM,但这是来自用户控件的Xaml代码的一部分:

  <UserControl.CommandBindings>
    <CommandBinding Command="New" 
            CanExecute="CanNewPageCommandExecute" Executed="OnNewPageCommandExecute" />
    <CommandBinding Command="NavigationCommands.PreviousPage" 
            CanExecute="CanPreviousPageCommandExecute" Executed="OnPreviousPageCommandExecute" />
    <CommandBinding Command="NavigationCommands.NextPage"
            CanExecute="CanNextPageCommandExecute" Executed="OnNextPageCommandExecute" />
    <CommandBinding Command="FirstPage"
            CanExecute="CanPreviousPageCommandExecute" Executed="OnFirstPageCommandExecute" />
    <CommandBinding Command="LastPage"
            CanExecute="CanNextPageCommandExecute" Executed="OnLastPageCommandExecute" />
    <CommandBinding Command="{x:Static local:student.saves}"
            CanExecute="CanSavePageCommandExecute" Executed="OnSavePageCommandExecute" />
    <CommandBinding Command="Delete"
            CanExecute="CanDeletePageCommandExecute" Executed="OnDeletePageCommandExecute" />
</UserControl.CommandBindings>

<Border Width="Auto" Height="Auto" BorderThickness="1" CornerRadius="2">
    <dxlc:LayoutControl Name="layoutControl1" Cursor="Arrow">
        <dxlc:LayoutGroup Orientation="Vertical" Height="303">
            <Border BorderThickness="1" Height="160" Width="160" BorderBrush="Black" Margin="0,0,0,20">
                <Border.Background>
                    <ImageBrush ImageSource="/college12;component/images/prime.jpg" Stretch="Uniform" TileMode="None" />
                </Border.Background>
                <Image Height="150" Name="studypixx" Stretch="UniformToFill" Width="150" UseLayoutRounding="False" DataContext="{StaticResource studentsViewSource}" Source="{Binding Path=studyphoto, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </Border>
            <Button Content="Upload Photo" Height="23" HorizontalAlignment="Center" Name="studentphotoupload" VerticalAlignment="Top" Width="116" ToolTip="Inserts photo" Background="#FFEC3B3B" BorderBrush="Black" Cursor="Hand" Click="studentphotoupload_Click" />
            <TextBox Name="photo_text" Width="160" HorizontalAlignment="Left" Height="20" IsHitTestVisible="False" Visibility="Visible" Text="{Binding Path=studyphoto, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" DataContext="{StaticResource studentsViewSource}" />
            <dxlc:LayoutGroup Name="layoutGroup3" View="GroupBox" Width="160" Height="62">
                <Button Content="NEW STUDENT" Height="23" HorizontalAlignment="Center" Name="addstudent" VerticalAlignment="Bottom" Width="93" Command="ApplicationCommands.New" Cursor="Hand" />
            </dxlc:LayoutGroup>
        </dxlc:LayoutGroup>
        <dxlc:LayoutGroup Orientation="Vertical">
            <dxlc:LayoutGroup Name="layoutGroup1" View="GroupBox" Height="238" UseLayoutRounding="True" Orientation="Vertical" Width="450" DragScrolling="False" Header="{Binding Path=surname}" DataContext="{StaticResource studentsViewSource}">
                <dxlc:LayoutGroup>
                    <dxlc:LayoutItem Label="First Name" Name="layoutItem1">
                        <dxe:TextEdit Name="first_name" FontFamily="Consolas" UseLayoutRounding="False" CharacterCasing="Upper" MaskSaveLiteral="True" Mask="[a-zA-Z]+" MaskType="RegEx" EditValue="{Binding Path=firstname, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=LostFocus}" />
                    </dxlc:LayoutItem>
                    <dxlc:LayoutItem Label="Middle Name" Name="layoutItem2">
                        <dxe:TextEdit Name="other_name" FontFamily="Consolas" UseLayoutRounding="False" CharacterCasing="Upper" MaskSaveLiteral="True" Mask="[a-zA-Z]+" MaskType="RegEx" EditValue="{Binding Path=othername, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                </dxlc:LayoutGroup>
                <dxlc:LayoutGroup>
                    <dxlc:LayoutItem Label="Surname" Name="layoutItem3">
                        <dxe:TextEdit Name="other_surname" FontFamily="Consolas" UseLayoutRounding="False" CharacterCasing="Upper" MaskSaveLiteral="True" Mask="[a-zA-Z]+" MaskType="RegEx" EditValue="{Binding Path=surname, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=LostFocus}"/>
                    </dxlc:LayoutItem>
                    <dxlc:LayoutItem Label="Email" Name="layoutItem10">
                        <dxe:TextEdit Name="email_address" EditValue="{Binding Path=email, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                </dxlc:LayoutGroup>
                <dxlc:LayoutGroup>
                    <dxlc:LayoutItem Label="Birth Date" Name="layoutItem4">
                        <dxe:DateEdit Name="birth_date" Mask="d/MM/yyyy" MaskSaveLiteral="False" MaskType="DateTime" MaskUseAsDisplayFormat="True" UseLayoutRounding="False" FontFamily="Consolas" FontSize="12" EditValue="{Binding Path=birthdate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                    <dxlc:LayoutItem Label="Enrol Date" Name="layoutItem5">
                        <dxe:DateEdit Name="enrol_date" MaskUseAsDisplayFormat="True" MaskType="DateTime" MaskSaveLiteral="False" Mask="d/MM/yyyy" UseLayoutRounding="False" FontFamily="Consolas" FontSize="12" EditValue="{Binding Path=enroldate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                </dxlc:LayoutGroup>
                <dxlc:LayoutGroup>
                    <dxlc:LayoutItem Label="Enrol NO." Name="layoutItem6">
                        <dxe:TextEdit Name="enrol_no" FontFamily="Consolas" FontSize="10" EditValue="{Binding Path=enrolnum, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                    <dxlc:LayoutItem Label="Phone NO." Name="layoutItem9">
                        <dxe:TextEdit Name="phone_no" Mask="(\d?\d?\d?\d)\d\d-\d\d-\d\d" MaskType="Regular" MaskUseAsDisplayFormat="True" FontFamily="Consolas" FontSize="10" MaskBeepOnError="False" MaskSaveLiteral="False" AcceptsReturn="True" AcceptsTab="True" EditValue="{Binding Path=phone, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                </dxlc:LayoutGroup>
                <dxlc:LayoutGroup>
                    <dxlc:LayoutItem Label="Address" Name="layoutItem7">
                        <dxe:TextEdit Name="address" FontFamily="Consolas" UseLayoutRounding="False" CharacterCasing="Upper" EditValue="{Binding Path=address, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                    <dxlc:LayoutItem Label="Town/City" Name="layoutItem8">
                        <dxe:TextEdit Name="town_city" FontFamily="Consolas" UseLayoutRounding="False" CharacterCasing="Upper" Mask="[a-zA-Z]+" MaskType="RegEx" EditValue="{Binding Path=town, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}"/>
                    </dxlc:LayoutItem>
                </dxlc:LayoutGroup>
            </dxlc:LayoutGroup>
            <dxlc:LayoutGroup Name="layoutGroup2" View="GroupBox" Width="450">
                <Button Content="FIRST" Height="23" HorizontalAlignment="Left" Name="firststudent" VerticalAlignment="Top" Width="75" Cursor="Hand" Command="NavigationCommands.FirstPage" />
                <Button Content="PREVIOUS" Height="23" HorizontalAlignment="Left" Name="previoustudent" VerticalAlignment="Top" Width="75" Command="NavigationCommands.PreviousPage" ToolTip="Preceding student record" Cursor="Hand" />
                <Button Content="NEXT" Height="23" HorizontalAlignment="Left" Name="nextstudent" VerticalAlignment="Top" Width="75" Command="NavigationCommands.NextPage" ToolTip="Next student record" Cursor="Hand" />
                <Button Content="LAST" Height="23" HorizontalAlignment="Left" Name="laststudent" VerticalAlignment="Top" Width="75" Cursor="Hand" Command="NavigationCommands.LastPage" />
                <Button Content="SAVE" Height="23" HorizontalAlignment="Left" Name="savestudent" VerticalAlignment="Top" Width="75" Cursor="Hand" Command="{x:Static local:student.saves}"/>
            </dxlc:LayoutGroup>
        </dxlc:LayoutGroup>
    </dxlc:LayoutControl>
</Border>

0 个答案:

没有答案