Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可见性绑定不适用于不同的PC';s_C#_Wpf_Binding_Visibility_Datatemplate - Fatal编程技术网

C# 可见性绑定不适用于不同的PC';s

C# 可见性绑定不适用于不同的PC';s,c#,wpf,binding,visibility,datatemplate,C#,Wpf,Binding,Visibility,Datatemplate,我对DataGrid的DataTemplate中的可见性绑定有问题 我对可见性的绑定是 Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" 我添加了一个显示IsAdmin值的文本框: <TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Bin

我对DataGrid的DataTemplate中的可见性绑定有问题

我对可见性的绑定是

Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}"
我添加了一个显示IsAdmin值的文本框:

<TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />
如果“IsAdmin”属性为false,则应折叠复选框(或任何内容)。它可以在我的机器(.NET 4.5.1)上正常工作,但如果我在另一台安装了.NET 4.0的机器上尝试,它将无法工作。这个项目的目标框架是.NET4.0


我做错了什么?这是一个.NET错误吗?有什么想法吗?谢谢

谢谢,这就是解决方案

<CheckBox Content="Test" Visibility="{Binding Source={x:Reference Hauptfenster}, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" />


现在它就像一个符咒

在TestConvert中运行调试。验证输入的内容。我猜另一台计算机上的
IsAdmin
为false。在转换器中验证
IsAdmin
的值,然后回溯查看为什么在您认为应该为真时它为假。也许您在另一台计算机上部署了不同的配置?我认为这在.net 4.5中得到了重大改进,这就是ElementName绑定工作的原因。在.NET4.0DataGrid中,列经常丢失LogicalTree,因此无法正确创建绑定。不过,为了确保它在两个.net世界都能工作,您可以将绑定源设置为x:reference Hauptfenster:)谢谢,这就是解决方案<代码>现在它就像一个符咒!
<Window x:Class="BindingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    x:Name="Hauptfenster">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="TestConvert" />
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="87*"/>
    </Grid.RowDefinitions>

    <ToggleButton Content="TestButton" Width="150" Height="30" Click="ToggleButton_Click" />

    <TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />

    <TextBlock Text="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin}" Width="100" Height="20" HorizontalAlignment="Left"  />

    <DataGrid x:Name="dgTest" Grid.Row="1">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Spalte 1">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Content="Test" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" />
                            <TextBlock Text="{Binding Spalte1}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Spalte 2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Content="Test" />
                            <TextBlock Text="{Binding Spalte2}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Spalte 3" Binding="{Binding Spalte3}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>
public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value == true) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<CheckBox Content="Test" Visibility="{Binding Source={x:Reference Hauptfenster}, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" />