Click here to Skip to main content
15,885,197 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Comments
Jason Gleim 3-Nov-12 13:57pm    
Without some code it is hard to figure out what is going on. However, if I were to guess, you will find that the data grid and the button are now in two different data contexts. Use a tool like Snoop, XAML Spy, or WPF Spy to look at the tab items when your app is running and you will likely see they are not in the same data context. You will probably have to explicitly set the binding to your view model either in code or in xaml with DataContext="{Binding yourViewModel}"
ompcj4u 4-Nov-12 14:44pm    
HiHear I put My Some Code and My Project
Can you See them and tell me more about my Problem?
Thank you

My Project's Link :
http://ompc.ir/MVVMTestApplication.zip

1 solution

Hi Jason
Excusme? What is Snoop? or XAML Spy or WPF Spy?

XML
<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>


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900