在WPF gridview中禁用特定行上的按钮

时间:2013-07-16 10:42:02

标签: c# wpf gridview

这是我生成gridview的代码

        User u1 = new User();
        User u2 = new User();

        for (int i = 0; i < files.Count; i++)
        {
            u1.Files = files[i];
            items.Add(u1);
        }

        for (int i = 0; i < sharedFiles.Count; i++)
        {
            u2.Files += sharedFiles[i];
            items.Add(u2);
        }
        gridview1.ItemsSource = items;
    }

    public class User
    {
        public string Files { get; set; }
    }

这是我的xaml页面

 <ListView.View>
            <GridView >
                <GridView.Columns>

                    <GridViewColumn Width="250" DisplayMemberBinding="{Binding Files}" ></GridViewColumn>

                    <GridViewColumn >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Download" Click="fileDownloadClick" CommandParameter="{Binding Files}"></Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Share"  Click="fileShareClick"  CommandParameter="{Binding Files}" ></Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView.Columns>

            </GridView>
 </ListView.View>  

我想知道是否可以在我的第二个for循环(u2)中禁用我的Share按钮,同时仍然启用(u1)中的Share按钮。目前两者都已启用。我目前正在使用它来检索特定角色的值

 private void fileShareClick(object sender, RoutedEventArgs e)
    {
        object share = ((Button)sender).CommandParameter;
    }

1 个答案:

答案 0 :(得分:0)

在Button上创建所需的DataTrigger。并将其与表示boolen类型值的属性绑定。像IsShareAllowed。

尝试这样的事情。

var procs = (from p in System.Diagnostics.Process.GetProcesses()

                     select new
                     {
                         Name = p.ProcessName,

                         IsShareAllowed = Convert.ToBoolean(r.Next(-1, 1))
                     }).ToList(); ;

        list.ItemsSource = procs;

的Xaml

 <ListView Name="list" Grid.Row="4">
            <ListView.View>
                <GridView >
                    <GridView.Columns>

                        <GridViewColumn Width="250" DisplayMemberBinding="{Binding Name}" />

                        <GridViewColumn >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Download" Click="fileDownloadClick" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Share"  Click="fileShareClick"  >
                                        <Button.Style>
                                            <Style TargetType="Button">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsShareAllowed}" Value="false">
                                                        <Setter Property="IsEnabled" Value="false"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Button.Style>                                        
                                    </Button>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                    </GridView.Columns>

                </GridView>
            </ListView.View>
        </ListView>