如何在GridView中选择项目时启用按钮
本文关键字:项目 启用 按钮 选择 GridView | 更新日期: 2023-09-27 18:28:51
我想在选择网格视图项目时启用按钮,以便更新metro应用程序中的GUI。该按钮也包含为列表视图项。下面是我想做的事情的代码片段。请帮忙。
<GridView Name="searchPanelGrid" SelectionMode="Single"
HorizontalAlignment="Left"
ScrollViewer.IsHorizontalScrollChainingEnabled="True"
ScrollViewer.IsVerticalScrollChainingEnabled ="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.VerticalScrollMode="Enabled"
ItemsSource="{Binding Source={StaticResource CollectionItems}}" Grid.Row="2">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Margin="6" Height="175" Width="150" Background="#FFFAFAFA">
<Grid.RowDefinitions>
<RowDefinition Height="85"/>
<RowDefinition Height="50"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<StackPanel Background="#FF0A56BF" Width="150" Height="85" Grid.Row="0">
<Image Source="{Binding Path=ThumnailUrl}" Stretch="UniformToFill" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</StackPanel>
<TextBlock Text="{Binding Path=VideoName}" TextWrapping="Wrap" Foreground="#FF017DD5" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" FontSize="12"/>
<Button x:Name="downloadButton" Grid.Row="3" Content="Download Video" HorizontalAlignment="Left" VerticalAlignment="Bottom" Style="{StaticResource DownloadButtonStyle}" Click="downloadButton_Click" IsEnabled="{Binding}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
无法绑定按钮属性IsEnabled。有什么建议吗??
我已经解决了这个问题,方法是在我的类中声明一个属性,通过该属性绑定collectonitems,并将该属性绑定到按钮isenabled属性,然后在SelectionChangeEvent上,我获得网格视图的选定项,并将按钮isenable属性值设置为true。它起作用了。
Xaml文件SearchPanel.Xaml
<GridView Name="searchPanelGrid" SelectionMode="Single"
HorizontalAlignment="Left"
ScrollViewer.IsHorizontalScrollChainingEnabled="True"
ScrollViewer.IsVerticalScrollChainingEnabled ="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.VerticalScrollMode="Enabled"
ItemsSource="{Binding Source={StaticResource CollectionItems}}" Grid.Row="2" SelectionChanged="searchPanelGrid_SelectionChanged">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Margin="6" Height="175" Width="150" Background="#FFFAFAFA">
<Grid.RowDefinitions>
<RowDefinition Height="85"/>
<RowDefinition Height="50"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<StackPanel Background="#FF0A56BF" Width="150" Height="85" Grid.Row="0">
<Image Source="{Binding Path=ThumnailUrl}" Stretch="UniformToFill" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</StackPanel>
<TextBlock Text="{Binding Path=VideoName}" TextWrapping="Wrap" Foreground="#FF017DD5" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" FontSize="12"/>
<Button x:Name="downloadButton" Grid.Row="3" Content="Download Video" HorizontalAlignment="Left" VerticalAlignment="Bottom" Style="{StaticResource DownloadButtonStyle}" Click="downloadButton_Click" IsEnabled="{Binding IsSelected}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
绑定到GridView 的ItemsSource属性的类
public class VideoInfo : INotifyPropertyChanged
{
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
private string thumnailUrl;
public string ThumnailUrl
{
get { return thumnailUrl; }
set
{
thumnailUrl = value;
NotifyPropertyChanged("ThumnailUrl");
}
}
private string videoName;
public string VideoName
{
get { return videoName; }
set
{
videoName = value;
NotifyPropertyChanged("VideoName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
关于GridView 的SelectionChangeEvent的.cs文件SearchPanel.xam.cs
private void searchPanelGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MovieInfo info = (e.AddedItems[0]) as MovieInfo;
info.IsSelected = true;
}