2

I'm working on my first UWP app and I want create a UI like this . For each list item (project) there'll be a set of buttons. For certain list items(projects) some of these buttons will be disabled some times. So I need to disable and change the image for such button in those list items(projects).

I tried to implement it using a list view like this. But I am not sure how I can enable/disable some of those buttons depending on the condition.

Tried adding a DataContextChanged event and trying to access the buttons there. But not sure how I can access those buttons.

Please let me know whether the following approach is correct or is there a better way to do what I am trying to achieve in the above image.

 <ListView x:Name="stepsListView" Margin="10,0,0,0" RequestedTheme="Dark" FontSize="24" Background="{StaticResource procedure_app_white}" Foreground="Black" BorderThickness="1.5" BorderBrush="Transparent" ItemsSource="{Binding projectList}"  HorizontalAlignment="Left">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>

    <!-- Item -->
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,0,0,1" BorderBrush="#c0c0c0">
                <Grid Width="auto" HorizontalAlignment="Stretch" DataContextChanged="Grid_DataContextChanged" >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="50"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock VerticalAlignment="Center" FontSize="30" Grid.Row="0" Grid.ColumnSpan="7" Text="{Binding projectName}" Foreground="{StaticResource procedure_app_orange_text }" />

                    <Button x:Name="warningButton" Width="40" Height="40" Grid.Column="1"  Grid.Row="1" Tag="{Binding projectId}" Click="warningButtonClick" Foreground="{StaticResource procedure_app_orange_text }">
                        <Button.Background>
                            <ImageBrush ImageSource="Asset/step_ncwr.png">
                            </ImageBrush>
                        </Button.Background>
                    </Button>
                    <Button x:Name="commentButton" Width="40" Height="40" Grid.Column="2"   Grid.Row="1"  Tag="{Binding projectId}" Click="CommentButtonClick" Foreground="{StaticResource procedure_app_orange_text }" IsTapEnabled="True">
                    <Button.Background>
                        <ImageBrush ImageSource="Asset/step_comment.png">
                        </ImageBrush>
                    </Button.Background>
                    </Button>
                    <Button x:Name="imageButton" Width="40" Height="40" Grid.Column="3"  Grid.Row="1"  Tag="{Binding projectId}" Click="ImageButtonClick" Foreground="{StaticResource procedure_app_orange_text }">
                        <Button.Background>
                            <ImageBrush ImageSource="Asset/step_image.png">
                            </ImageBrush>
                        </Button.Background>
                    </Button>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
Yael
  • 1,566
  • 3
  • 18
  • 25
Madhu
  • 1,209
  • 2
  • 22
  • 28

1 Answers1

5

The answer is to variable dependent on what structure you've gone with so I am going to make some assumptions and roll with it.

First I am going to assume your ViewModel has an ObservableCollection called ProjectList and that this ProjectList is made up of ProjectModel's

ProjectModel.cs

public class ProjectModel : INotifyPropertyChanged{
      private bool _isNcwrEnabled;
      public bool IsNcwrEnabled{
          get{ return _isNcwrEnabled; }
          set{ _isNcwrEnabled = value; OnPropertyChanged("IsNcwrEnabled"); }
      }
      private bool _isCommentEnabled;
      public bool IsCommentEnabled{
          get{ return _isCommentEnabled; }
          set{ _isCommentEnabled= value; OnPropertyChanged("IsCommentEnabled"); }
      }
      private bool _isImageEnabled;
      public bool IsImageEnabled{
          get{ return _isImageEnabled; }
          set{ _isImageEnabled= value; OnPropertyChanged("IsImageEnabled"); }
      }

    public void OnPropertyChanged(String prop)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

In your ViewModel you should have

ObservableCollection<ProjectModel> ProjectList {get; private set; }

Finally in your View

    <Button IsEnabled="{Binding IsNcwrEnabled}" x:Name="warningButton" Width="40" Height="40" Grid.Column="1"
            Grid.Row="1" Tag="{Binding projectId}" Click="warningButtonClick" 
            Foreground="{StaticResource procedure_app_orange_text }">
         <Button.Background>
             <ImageBrush ImageSource="Asset/step_ncwr.png"/>
         </Button.Background>
    </Button>
    <Button IsEnabled="{Binding IsCommentEnabled}" x:Name="commentButton" Width="40" Height="40" Grid.Column="2"
            Grid.Row="1"  Tag="{Binding projectId}" Click="CommentButtonClick" 
            Foreground="{StaticResource procedure_app_orange_text }" IsTapEnabled="True">
         <Button.Background>
             <ImageBrush ImageSource="Asset/step_comment.png"/>
         </Button.Background>
     </Button>
     <Button IsEnabled="{Binding IsImageEnabled}" x:Name="imageButton" Width="40" Height="40" Grid.Column="3"
             Grid.Row="1"  Tag="{Binding projectId}" Click="ImageButtonClick" 
             Foreground="{StaticResource procedure_app_orange_text }">
         <Button.Background>
             <ImageBrush ImageSource="Asset/step_image.png"/>
         </Button.Background>
     </Button>

Summary of Changes

  1. The models in the collection that your ListView is bound to needs to contain enabled properties for your Buttons to bind to
  2. In your View, bind your Buttons to your new properties
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Thank you very much Anthony for the detailed answer. So if I want to change the image when disabled. I need to define another property in the ProjectModel like ncwrImage and set the image depending on the condition and set like ImageSource="{Binding ncwrImage }" in the ImageBrush . Is that correct? – Madhu Jul 13 '16 at 15:28
  • If you wanted to change the images I would create a Data trigger that switched the image source based on the model's enabled property – DotNetRussell Jul 13 '16 at 15:30
  • @Madhu if this answer works for you please don't forget to check mark and upvote so others can use it – DotNetRussell Jul 13 '16 at 15:35
  • Thanks Anthony, I'll mark this as the answer. But still I couldn't find a way to do these two things.
    1. how to hide some buttons using binding. Looks like I need to set Visibility=collapse. But not sure how to do it using the bool I have. I tried this http://stackoverflow.com/questions/7000819/binding-a-button-visibility-to-bool-value-in-viewmodel but it doesn't seem to work on uwp.
    2. How to change the ImageBrush ImageSource in the above code, using that bool. I looked at the link you have provided, but those style triggers are not supported in uwp. :-(
    – Madhu Jul 14 '16 at 12:12
  • @Madhu if you have more questions then just post them in new Stack Overflow questions :-) – DotNetRussell Jul 14 '16 at 12:13
  • 1
    I added a new question as you suggested http://stackoverflow.com/questions/38374143/uwp-app-show-hide-button-using-binding – Madhu Jul 14 '16 at 12:26