简体   繁体   English

Listview项目内的UWP按钮

[英]UWP buttons inside Listview items

I'm working on my first UWP app and I want create a UI like this 我正在开发我的第一个UWP应用,我想创建一个这样的UI . 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. 尝试添加DataContextChanged事件并尝试访问那里的按钮。 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>

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 首先,我假设您的ViewModel具有一个名为ProjectListObservableCollection ,并且此ProjectListProjectModel

ProjectModel.cs 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 在您的ViewModel您应该拥有

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

Finally in your View 最终在您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 ListView绑定到的集合中的模型需要包含使Buttons绑定到的已启用属性。
  2. In your View , bind your Buttons to your new properties View ,将Buttons绑定到新属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM