我有一個列表視圖,其中顯示項目名稱和一些按鈕爲每個項目做不同的動作,如添加評論查看該項目的圖像等。根據項目一些項目將有些按鈕有時會禁用某些按鈕。有些項目中某些按鈕不可見。所以在這段代碼中我想使用數據綁定來實現兩件事情。UWP應用程序顯示/隱藏按鈕使用綁定
根據ProjectModel的一些布爾變量,我需要更改按鈕的可見性。我試過這個Binding a Button's visibility to a bool value in ViewModel,但它似乎不適用於uwp。
對於某些項目,當該選項被禁用時,我需要顯示不同的彩色圖像。所以我需要根據ProjectModel的布爾變量來更改ImageBrush的ImageSource。爲此,我嘗試了這個Change image using trigger WPF MVVM,這些樣式觸發器不適用於uwp。
請讓我知道如何在UWP應用程序中輕鬆完成這些操作。這是我的第一個UWP應用程序,我對這些概念很陌生。
<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>
所有禁用的圖像都是相同的圖像嗎? –
@AnthonyRussell例如在項目1中,評論按鈕將被禁用。所以我需要爲評論按鈕顯示另一個圖標,它顯示爲灰色禁用。在項目2中,圖像按鈕將被禁用。在項目3中,我需要隱藏評論按鈕。等等。我想做兩件事。隱藏一些按鈕並更改某些按鈕的圖像。這些將在該項目上有所不同。對於每個按鈕,將有兩個圖像爲橙色圖像和灰色圖像。 – Madhu
你不應該創建一個灰色。只要禁用按鈕,它應該灰色縮放您的圖像 –