Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to WPF, and i have to build an application where there are Multiple tabs in a window, and There are save and save all buttons in the window. So Changes in any of the controls(like Textbox text change or check box check changed or dropdown selected index change), the save button should be enabled. On load the save button should be disabled as no changes made on load.

I tried multiple approaches like implementing InotifyPropertyChange and ICommand, but not able to achieve this.

Can anyone please help me in this regard? b

Below is a code snippet of my mainwindow xaml page.

What I have tried:

<Page x:Class="Example1.MainPage"
      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:local="clr-namespace:Example1"
       xmlns:vm="clr-namespace:Example1.ViewModels"
        xmlns:m="clr-namespace:Example1.Models"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="MainPage">
    <Page.Resources>
        <m:Config x:Key="Config"/>
        <vm:ViewModelBase x:Key="ViewModel"/>
    </Page.Resources>
    <StackPanel>
        <Grid>
            <ToolBar x:Uid="ToolBar_1" VerticalAlignment="Top" Height="56" Background="{x:Null}" >
                <Button x:Uid="btnSaveas" x:Name="btnSaveas" Content="Save" FontWeight="Bold" Width="146" Height="36" BorderBrush="Transparent" Foreground="White" Command="{Binding simplpComm, Source={StaticResource ViewModel}}">
                    <Button.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="0"/>
                            <GradientStop Color="White" Offset="1"/>
                        </LinearGradientBrush>
                    </Button.Background>
                </Button>

            </ToolBar>
            <TabControl  x:Uid="TabControl_1" FontFamily="Tahoma" FontSize="14" Grid.RowSpan="2" Margin="0,61,0,46">
                <TabItem x:Uid="tabConfig" x:Name="tabConfig" Margin="0" Loaded="tabConfig_Loaded" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                    <TabItem.Header >
                        <Label x:Name="lblConfigure" HorizontalAlignment="Stretch" Content="User"></Label>
                    </TabItem.Header>
                    <ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                        <Grid x:Uid="gridProperties" x:Name="gridProperties">
                        </Grid>
                    </ScrollViewer>
                </TabItem>
            </TabControl>
        </Grid>
    </StackPanel>
</Page>
Posted
Updated 5-Jan-18 7:39am

You are using the MVVM pattern. The UI is only a reflection of the state of the ViewModel and model data. So when you are looking at what you are trying to do, don't think in terms of what the controls are doing, think in terms of the state of the data. So the validation state, which is where the data state is checked, is what the controls bind to to manage their states.

The ViewModel, and any data models, are not supplied with the question, so it is hard to help you any further with the issues in your code.
 
Share this answer
 
You need a RelayCommand [^] implementation of the ICommand.

You can then initialize the ICommand like the following:
C#
this.SaveAllCommand = new RelayCommand(SaveAllAction, () => /* Any condition for enable / disable of button goes here */);

Whenever something changes you have to update the button:
C#
this.SaveAllCommand.RefreshCanExecute();

Don't forget to bind the command to the button
<Button Content="Save All" Command="{Binding SaveAllCommand}" />
 
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