2013-08-26 33 views
0

我有一个DataGrid复选框&其他文本框。绑定DataGrid基于复选框属性的TextBox启用

<DataGrid AutoGenerateColumns="False" Height="170" Name="dataGrid1" Width="527" OpacityMask="#FF161A1A" BorderBrush="#FFB7B39D" Background="LightYellow" RowBackground="LightGray" AlternatingRowBackground="#FFFFFFF5" BorderThickness="10" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserSortColumns="False" FontFamily="Segoe UI" FontSize="13" CanUserAddRows="False"> 

     <DataGrid.Columns> 
      <DataGridCheckBoxColumn Header="" Binding="{Binding BoolProperty, Mode=TwoWay}" /> 
      <DataGridTextColumn Header="" Binding="{Binding header}" MinWidth="108" IsReadOnly="True" /> 
      <DataGridTextColumn Header="Number of Cases" Binding="{Binding cases}" > 
      <DataGridTextColumn.EditingElementStyle> 
        <Style TargetType="TextBox"> 
         <Setter Property="IsEnabled" Value="{Binding Path=BoolProperty, Mode=TwoWay}" /> 
       </Style> 
      </DataGridTextColumn.EditingElementStyle> 
      </DataGridTextColumn> 

checkboxcolumn绑定到“BoolProperty”。如果BoolProperty为false,我希望文本框的“Number of Cases”被禁用,如果BoolProperty为true,则启用它。我尝试在TExtBox中添加IsEnabled,但它不起作用。我哪里错了?

+0

你为什么要编辑编辑风格? –

+0

@MArk,看了很多后,我从这个网站的解决方案得到了这个想法 - http://wpf.codeplex.com/discussions/44656 – Tvd

回答

1

对于仅限XAML的方法,请改为使用模板列。 IsReadOnlyisn't bindable at the cell level。由于该链接不提供执行,我会。

<DataGridTemplateColumn> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=myProperty}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <TextBox IsEnabled="{Binding Path=myBool}" Text="{Binding Path=myProperty, Mode=TwoWay}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
</DataGridTemplateColumn> 
+0

这没有什么区别。无论是否选中复选框,我都可以在Number of Cases文本框中输入值。 Tvd

+0

@Tvd您是对的,更新了什么工作 – Shoe

+0

对不起,添加这段代码 - 我是说它在TextBoxCol之间,还是在DataCols之前或之前?我在这件事上效率不高。谢谢。 – Tvd

0

我在一个项目中使用了我的DataGridLoadingRow事件来检查特定状态。也许这样可能会有所帮助:

void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
    checkRow(e.Row); 
} 

private void checkRow(DataGridRow dgRow) 
{ 
    if (dgRow == null) 
     return; 

    var item = dgRow.Item as MyItemClass; 
    if (item != null && item.BoolProperty) 
    { 
     ... 
    } 
    else 
    { 
     ... 
    } 
} 

在你的情况下,你可以在if-else构造中启用/禁用你的单元。

希望它有帮助。

+0

那么如果当用户选择/取消选择复选框,那么当我不认为LoadingRow事件将被执行。 !!!! – Tvd

+0

没错,那么你可以在你的'DataGrid'的'CellEditEnding'事件中添加一个处理程序,它也调用'checkRow()'函数。 –

相关问题