2017-05-17 107 views
-1

我想将一些整数转换为DataGrid列的ReadOnly值。对于这一点,我做了以下内容:C#/ WPF - 转换器不会被调用?

namespace TanulmanyiRendszer.Admin.ViewModel 
{ 
    public class GradeToReadOnlyConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      Boolean IsReadOnly = (Int32.Parse((String)value) < 2) ? true : false; 
      return IsReadOnly; 
     } 
     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return null; 
     } 
    } 
} 

XAML浏览

<Window x:Class="TanulmanyiRendszer.Admin.View.MainWindow" 
    <!-- ETC --> 
    xmlns:viewModel="clr-namespace:TanulmanyiRendszer.Admin.ViewModel" 
    Title="Courses" Height="600" Width="500"> 
     <Window.Resources> 
      <viewModel:GradeToReadOnlyConverter x:Key="converter" /> 
     </Window.Resources> 
     <!-- ETC --> 
     <DataGrid Name="studentGrid" Grid.Row="3" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding Students, Mode=TwoWay}" > 
      <DataGrid.Columns> 
       <DataGridTextColumn IsReadOnly="{Binding StudentGrade, Converter={StaticResource converter}}" Header="Student's grade" Binding="{Binding StudentGrade}"/> 
      </DataGrid.Columns> 
     </DataGrid> 
</Window> 

然而,这并没有在所有的工作。转换器永远不会被调用。我在这里错过了什么?

+2

但'绑定=“{绑定StudentGrade}”'工作? – Clemens

+0

@Clemens是的,exaclty。等级显示出来,但我可以编辑它们而不考虑价值。 –

+0

检查输出窗口,您必须得到此错误:找不到目标元素的控制FrameworkElement或FrameworkContentElement。 BindingExpression:路径= StudentGrade;的DataItem = NULL;目标元素是'DataGridTextColumn'(HashCode = 15006601);目标属性是'IsReadOnly'(类型'布尔')。相同的原因和解决方案可以在这里找到:http://stackoverflow.com/questions/7660967/wpf-error-cannot-find-governing-frameworkelement-for-target-element –

回答

0

但是,这根本不起作用。

这是在这种情况下的预期行为。这背后的原因是Datagrid列不是可视树的一部分,这就是Datagrid列没有连接到Datagrid的Datacontext的原因。因此与Datagrid Column的IsReadOnly属性绑定将不起作用。此方案的一个好的解决方案(黑客/解决方法)可以在here找到。

让我知道你是否需要任何帮助与解决方案提供的链接。

1
<DataGridTextColumn Header="Student's grade" Binding="{Binding StudentGrade}"> 
    <DataGridTextColumn.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="IsHitTestVisible" Value="{Binding StudentGrade, Converter={StaticResource converter}}" /> 
     </Style> 
    </DataGridTextColumn.CellStyle> 
</DataGridTextColumn> 

试着改变DataGridCell的风格。 Setter属性可能是'IsHitTestVisible'或'IsEnabled'

0

我在这里丢失了什么?

一个DataGridTextColumn没有继承任何DataContext这意味着你不能将它的IsReadOnly属性直接绑定到StudentGrade源属性的事实。

请参考@Thomas Levesque的的博客文章有关更多信息,以及如何你可能通过使用BindingProxy类,从Freezable继承和捕捉DataContext解决您的问题:https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

虽然只是为该列定义自定义EditingElementStyle会更容易。试试这个:

<DataGridTextColumn IsReadOnly="{Binding StudentGrade, Converter={StaticResource converter}}" 
        Header="Student's grade" Binding="{Binding StudentGrade}"> 
    <DataGridTextColumn.EditingElementStyle> 
     <Style TargetType="TextBox"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding StudentGrade, Converter={StaticResource converter}}" Value="True"> 
        <Setter Property="BorderThickness" Value="0" /> 
        <Setter Property="IsEnabled" Value="False" /> 
        <Setter Property="Background" Value="Transparent" /> 
        <Setter Property="TextWrapping" Value="Wrap" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGridTextColumn.EditingElementStyle> 
</DataGridTextColumn>