本文介绍了TabItems中的WPF MVVM问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我在程序中使用wpf和mvvm模式时,如果ID等于零,则在datagrid中选择一行,然后启用保存按钮",然后可以保存记录;

因此,如果我将datagrid放在一个TabItem中,并且将按钮放到另一个TabItem中(两个TabItem都加长了一个TabControl),则ICommand CanExecute方法不起作用.

我使用了MS MVVM Light Toolkit;

借口英语不好

谢谢

Hello Every Body

I''m using wpf and mvvm pattern, in my program when you select a row in datagrid if ID equal zero Save Button Enabled and you can save the record;

so if I put datagrid in one TabItem and the Button To Othere TabItem(both of TabItems are blonged to one TabControl) ICommand CanExecute Method Not Working.

I used MS MVVM Light Toolkit ;

Excusme To bad English

Thank you

推荐答案

<usercontrol x:class="MVVMTestApplication.Views.CustomerView" xmlns:x="#unknown">
             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"
             xmlns:vm="clr-namespace:MVVMTestApplication.ViewModels"
             mc:Ignorable="d" FontFamily="Tahoma"
             d:DesignHeight="431" d:DesignWidth="483" FocusManager.FocusedElement="{Binding txtname}" xmlns:my="clr-namespace:Navigation;assembly=Navigation">
    <usercontrol.resources>
        <vm:customerviewmodel x:key="CVM" xmlns:vm="#unknown" />
        <collectionviewsource x:key="CVS" />
    </usercontrol.resources>

    <usercontrol.inputbindings>
        <keybinding key="N" modifiers="Control" command="{Binding AddNewCommadn}" />
        <keybinding key="S" modifiers="Control" command="{Binding InsertCommand}" />
    </usercontrol.inputbindings>

    <grid flowdirection="RightToLeft" datacontext="{Binding Source={StaticResource CVM}}">
        <tabcontrol background="Wheat" margin="3,41,3,3">
            <tabitem header="Review">
                <datagrid canuseraddrows="False" itemssource="{Binding CVS.View}" selecteditem="{Binding CurrentCustomer}" autogeneratecolumns="False" margin="10" name="dataGrid1" background="White">
                    <datagrid.columns>
                        <datagridtextcolumn header="Name" binding="{Binding CustomerName,UpdateSourceTrigger=PropertyChanged}" width="100" />
                        <datagridtextcolumn header="Family" binding="{Binding Path=CustomerFamily,UpdateSourceTrigger=PropertyChanged}" width="100" />
                        <datagridtextcolumn header="Phone" binding="{Binding Path=CustomerPhone,UpdateSourceTrigger=PropertyChanged}" width="100" />
                    </datagrid.columns>
                </datagrid>
            </tabitem>
            <tabitem header="Edit Section">
                <groupbox>
                    <grid datacontext="{Binding CurrentCustomer}">
                        <textbox height="23" text="{Binding Path=CustomerName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" horizontalalignment="Left" margin="70,18,0,0" name="txtname" verticalalignment="Top" width="120" inputlanguagemanager.inputlanguage="fa-ir" />
                        <textbox height="23" text="{Binding Path=CustomerFamily,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" horizontalalignment="Left" margin="70,47,0,0" name="textBox2" verticalalignment="Top" width="120" />
                        <textbox height="23" text="{Binding Path=CustomerPhone,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" horizontalalignment="Left" margin="70,76,0,0" name="textBox3" verticalalignment="Top" width="120" />
                        <textblock height="23" horizontalalignment="Left" margin="12,18,0,0" name="textBlock1" text="Name :" verticalalignment="Top" />
                        <textblock height="23" horizontalalignment="Left" margin="12,47,0,0" name="textBlock2" text="Family :" verticalalignment="Top" />
                        <textblock height="23" horizontalalignment="Left" margin="12,76,0,0" name="textBlock3" text="Phone :" verticalalignment="Top" />



        <button command="{Binding AddNewCommand}" commandparameter="{Binding ElementName=txtname,Path=Text}" content="Insert" height="23" horizontalalignment="Left" margin="238,12,0,0" name="button2" verticalalignment="Top" width="75" />
        <button command="{Binding InsertCommand}" content="Save" height="23" horizontalalignment="Left" margin="319,12,0,0" name="button1" verticalalignment="Top" width="75" />
                    </grid>
                </groupbox>

            </tabitem>
        </tabcontrol>
        <!--<Button Command="{Binding NewxItemCommand}" Content="Next" Height="23" HorizontalAlignment="Left" Margin="55,4,0,0" Name="button3" VerticalAlignment="Top" Width="75" />-->
        <!--<Button Command="{Binding BackItemCommand}" Content="Back" Height="23" HorizontalAlignment="Left" Margin="136,4,0,0" Name="button4" VerticalAlignment="Top" Width="75" />-->


    </grid>
</usercontrol>



我通过"NewxItemCommand"和"BackItemCommand"在Datagrid中选择不同的行
如果AddNewCommand和InsertCommand不在TabItem中,则它们可以正常工作,但是,如果我将它们添加到TabItem中,它们将不会自动启用.



I Select Different Rows in Datagrid By "NewxItemCommand" and "BackItemCommand"
if AddNewCommand And InsertCommand Not be in TabItem,they Work Properly But,if i add they into TabItem They wont Disable Automatic.


这篇关于TabItems中的WPF MVVM问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:37