C#/WPF-转换器不';你没接到电话吗?

C#/WPF-转换器不';你没接到电话吗?,c#,wpf,converter,C#,Wpf,Converter,我想将一些整数转换为DataGrid列的只读值。为此,我将做以下工作: namespace TanulmanyiRendszer.Admin.ViewModel { public class GradeToReadOnlyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

我想将一些整数转换为DataGrid列的只读值。为此,我将做以下工作:

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;
        }
    }
}
名称空间TanulmanyiRendszer.Admin.ViewModel
{
公共类GRADEONLYCONVERTER:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
布尔值IsReadOnly=(Int32.Parse((字符串)值)<2)?true:false;
返回为只读;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回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>

然而,这根本不起作用。转换器从未被调用。我错过了什么

然而,这根本不起作用

这是本场景中的预期行为。这背后的原因是Datagrid列不是可视化树的一部分,这就是Datagrid列未连接到Datagrid的Datacontext的原因。因此,使用Datagrid列的IsReadOnly属性进行绑定将不起作用。可以找到一个很好的解决方案(黑客/变通方法)

如果您需要有关所提供链接的解决方案的任何帮助,请告诉我。


<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属性可以是“IsitTestVisible”或“IsEnabled”

我错过了什么

DataGridTextColumn
不继承任何
DataContext
,这意味着您无法将其
IsReadOnly
属性直接绑定到
StudentGrade
源属性

请参阅@Thomas Levesque的博客文章,了解更多关于这方面的信息,以及如何使用继承自
Freezable
并捕获
DataContext
BindingProxy
类来解决问题

不过,只需为列定义一个自定义的
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>


但是
Binding=“{Binding StudentGrade}”
起作用了吗?@Clemens是的,没错。成绩会显示出来,但我可以编辑它们,而不考虑其值。检查输出窗口,您一定会遇到以下错误:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=StudentGrade;DataItem=null;目标元素为“DataGridTextColumn”(HashCode=15006601);目标属性为“IsReadOnly”(类型为“Boolean”)。原因和解决方案如下: