如果在GridView中选择了项目,如何启用按钮

时间:2013-08-23 15:19:41

标签: c# gridview microsoft-metro

我想在选择网格视图项时启用按钮,以便在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。有什么建议我怎么做??

1 个答案:

答案 0 :(得分:2)

我通过在我的Class中声明一个属性来解决这个问题,通过该属性绑定collectionitems并将该属性绑定到button isenabled属性,然后在SelectionChangeEvent上我获得网格视图的选定项并将按钮isenabled属性值设置为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));
        }
    }
}

.cs文件SearchPanel.xaml.cs在GridView的SelectionChangeEvent上

private void searchPanelGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MovieInfo info = (e.AddedItems[0]) as MovieInfo;
        info.IsSelected = true;
    }