2017-08-29 76 views
0

我想要一个只有在DataGrid中的单元格被选中时才启用的按钮。当SelectionUnit =“Cell”时,为什么这个触发器不工作

此行XAML的当DataGrid的SelectionUnit设置为“FullRow”作品:

IsEnabled="{Binding ElementName=dataGrid, Path=SelectedItems.Count}" 

它不能正常启用在DataGrid的SelectionUnit设置按钮“细胞”

任何想法为什么发生这种情况?

是否有任何工作良好的解决方法?

感谢

+0

怎么样'IsEnabled =“{Binding ElementName = dataGrid,Path = SelectedCells.Count}”'? –

+0

两者都无效.. –

+1

选择单元格时,“SelectedCells.Count”的值是否为非零值?如果是这样,你可以给Button提供一个带有DataTrigger的样式,当'{Binding ElementName = dataGrid,Path = SelectedCells.Count}'计算为零时禁用该按钮。或者,你可以写一个整数到布尔[值转换器](https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v = vs.110).aspx)。 –

回答

1

DataGrid的SelectedCells.Count在DataGrid被细胞选择应该返回一个非零值。 在Button的风格可以绑定到,并在Value="0"时禁用该按钮。

1

事实证明DataGrid的SelectedCells.Count属性没有实现INotifyPropertyChanged。

我的绑定无法通知我的数据触发时选择了改变细胞的数量,数据触发值始终为0(即使代码隐藏显示其他)...

我结束了将我的数据触发器绑定到跟踪DataGrid的选定行索引的内部属性,并检查它是否等于-1。

  <Button.Style> 
       <Style TargetType="Button"> 
        <Setter Property="IsEnabled" Value="True" /> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding SelectedRowIndex}" Value="-1"> 
          <Setter Property="IsEnabled" Value="False" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Button.Style>