0

I have a situation here.

I have one ListView with check boxes. check boxes are defined as data template. I have another stack panel and it contains one button. My requirement is that, initially when the window loads, none of the check boxes are selected and button should be disabled. when user clicks on any one of the checkbox, button should be enabled. when user unchecks all the checkboxes,button should be disabled.

i am using MVVM pattern with cattle framework.With the help of events,i could able achieve this functionality.

but my quesion is, can we able to handle this scenario in xaml itself. May be using triggers or converters?

I am new to WPF and no deep knowledge about WPF. Any help is appreciated.

I have tried using triggers. but since the checkboxes are inside ListView and defined as data template i am not able to access element name of checkbox from button style.

Rough outline is as follows

<ListView x:Name="MylistView" ItemsSource="{Binding Collection}" Height="250" 
              SelectionMode="Single" 
               BorderBrush="Transparent">
        <ListView.ItemTemplate>
            <DataTemplate>
                <CheckBox  x:Name="Checkbox" Content="{Binding Description}" IsChecked="{Binding IsEnable, Mode=TwoWay}" />
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

<StackPanel Orientation="Horizontal">
        <Button x:Name="Save" Content="{l:Language Key=SAVE}" Width="50" Height="25" Margin="10" 
                 Command="{Binding Updpate, Source={StaticResource proxy}}">
<Button.Style>
//How do i refer checkbox from this level. I get only "Mylistview" control    at this level
   </Button.Style>
  </Button>
</StackPanel>

thanks in advance

1 Answer 1

1

You have to declare a property Visibility with PropertyChanged event implemented as:

private bool _Visibility;
public bool Visibility
 { get 
      {
         return _Visibility ; 
      }
     set
      {
          _Visibility  = value;
           RaisePropertyChanged("Visibility");
      }
  }

internal void RaisePropertyChanged(string prop)
 {
     if (PropertyChanged != null)
      { 
        PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
      }
 }


public event PropertyChangedEventHandler PropertyChanged;

Design

<Button Name="btnsdf"  Width="35" Height="35" Style="{DynamicResource MetroCircleButtonStyle}" ToolTip="Add Denomination"  >
     <Image Width="30" Source="/Images/icons/add.png"  Margin="0,1,0,0"></Image>
     <Button.Resources>
          <Style TargetType="Button">
               <Style.Triggers>
                    <DataTrigger Binding="{Binding Visibility,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="true">
                         Setter Property="Visibility" Value="Visible"></Setter>
                     </DataTrigger>
                     <DataTrigger Binding="{Binding Visibility,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="false">
                         <Setter Property="Visibility" Value="Collapsed"></Setter>
                     </DataTrigger>
                </Style.Triggers>
            </Style>
       </Button.Resources>
  </Button>

You need to set IsEnabled on Checkbox Checked and unchecked event. I hope this will give you an idea.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! I can do this way but due to some implementation, IsChecked is bound to property defined in "Model" rather than in ViewModel. Data source for list view is defined in ViewModel. Button IsEnabled is bound in view model. So currently i am using events to communicate from model to view model when checkbox checked/unchecked.This is serving my purpose. But instead of event notification, is there any way i can handle in xaml and viewmodel? If "IsChecked" is not defined in Model, this answer would have fulfilled my requirement
You can implement RaisePropertyMethod in your model also. No necessity to declare it in ViewModel.
here DataSource is in ViewModel and IsChecked proeprty is defined in Model . So there has to be some communication from Model(IsChecked) to viewModel(DataSource) to enable and disable button since it depends on number of items in Listview. Instead of that cant we just do it in xaml ? Like , is there any way in which can we define triggers to enable and disable by reading listview checkbox items from xaml itself? or may be by using converters.
But You can get the value of IsChecked property in your view model. I don't think there should be any problem.
True. Currently IsChecked is bound to model and i am not supposed to change this.If it is bound to viewModel there would have been no problem. Is there a way where i can get change notfications in viewModel when checkbox checked\unchecked.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.