1

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

4

1 回答 1

2

我通过在我的类中声明一个属性来解决这个问题,通过该属性绑定集合项并将该属性绑定到按钮 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));
        }
    }
}

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

private void searchPanelGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MovieInfo info = (e.AddedItems[0]) as MovieInfo;
        info.IsSelected = true;
    }
于 2013-08-26T08:23:05.443 回答