【问题标题】:How to disable some button of wpf datagrid如何禁用wpf datagrid的某些按钮
【发布时间】:2012-09-27 10:30:17
【问题描述】:

我有一个 WPF 数据网格。其中有一列包含编辑按钮,例如如何以编程方式启用或禁用数据网格的某些按钮

 <DataGrid AutoGenerateColumns="True" Height="80" Margin="2,-4,8,0" Name="grdQHDRShowAll" VerticalAlignment="Top" ItemsSource="{Binding}" IsReadOnly="True" SelectionChanged="grdQHDRShowAll_SelectionChanged">
                                        <DataGrid.Columns>

                                            <DataGridTemplateColumn>
                                                <DataGridTemplateColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <Button Click="Qhdr_CreateOrder_Click"  CommandParameter="{Binding Path=QuoteNo}" >Create Order</Button>
                                                    </DataTemplate>
                                                </DataGridTemplateColumn.CellTemplate>
                                            </DataGridTemplateColumn>
                                        </DataGrid.Columns>
                                    </DataGrid>

上面的代码有一个带按钮的列,所以我想知道如何禁用行中的某些按钮取决于其他列数据,如第二列的真假值,如果第二列第一行的数据为真,那么第一列第一行按钮应该是启用方式取决于其他列的任何列如何启用/禁用数据网格的按钮?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    如果您使用 MVVM 模式,您可以将 Button 的 IsEnabled 属性绑定到 VM 的布尔属性,或者使用转换器将其转换为布尔类型:

    <Button IsEnabled="{Binding Path=BoolProp}"/>
    

    如果没有,您可以使用 elementbinding 将其绑定到元素:

    <Button IsEnabled="{Binding ElementName=CellName, Path=BoolProp}"/>
    

    【讨论】:

      【解决方案2】:
        <Button Content="Approve" VerticalAlignment="Center" Height="23" Width="90" FontWeight="Bold" Command="{Binding ApprovedCommand}" Margin="5,0,0,0" IsEnabled="{Binding IsEnabled,Mode=TwoWay}"/>
      

      在 MVVM 中,您创建一个属性并根据条件将 IsEnabled 设为 true 或 false

      【讨论】:

      • 你确定模式应该是TwoWay吗?在将其与等于TwoWay 的模式绑定时,我希望InvalidOperationException
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多