Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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# 设计时空引用错误_C#_Wpf_Xaml - Fatal编程技术网

C# 设计时空引用错误

C# 设计时空引用错误,c#,wpf,xaml,C#,Wpf,Xaml,在我的WPF应用程序中,我使用 {Binding Source={x:Reference myComboBox} ,Path=SelectedItem} 我在DataGrid列中执行此操作 这是在设计时(不是运行时)引发空引用异常。有没有办法解决这个问题,或者我可以通过其他方式访问所选项目 组合框: <ComboBox x:Name="myComboBox" ItemsSource="{Binding MyItems}"

在我的WPF应用程序中,我使用

{Binding Source={x:Reference myComboBox} ,Path=SelectedItem}
我在DataGrid列中执行此操作

这是在设计时(不是运行时)引发空引用异常。有没有办法解决这个问题,或者我可以通过其他方式访问所选项目

组合框:

<ComboBox         x:Name="myComboBox"
                  ItemsSource="{Binding MyItems}"
                  DisplayMemberPath="Name"
                  SelectedItem="{Binding SelectedItem}"
                   />

DataGridTextColumn:

<DataGridTextColumn HeaderStyle="{DynamicResource myStyle}"  Visibility="{Binding Source={x:Reference myComboBox} ,Path=SelectedItem, Converter={StaticResource ConvertSomething}, ConverterParameter={StaticResource Something}}" Header="MyHeader " Width="*" Binding="{Binding Path=MyBindingName}" />

为什么不试试
{Binding ElementName=myComboBox,Path=SelectedItem}
在本例中,请尝试使用继承
数据上下文的Freezable类型。现在我们不需要参考
组合框
,因为我们将在
数据上下文
中有一个属性。我认为,这是一个更普遍的解决方案:

绑定代理

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get 
        {
            return (object)GetValue(DataProperty); 
        }

        set
        {
            SetValue(DataProperty, value);
        }
    }

    public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data",
                                                                                typeof(object),
                                                                                typeof(BindingProxy));
}
<DataGrid>
    <DataGrid.Resources>
        <local:BindingProxy x:Key="bindingProxy" Data="{Binding}" />
    </DataGrid.Resources>

    <DataGrid.Columns>
        <DataGridTextColumn Visibility="{Binding Path=MySelectedItem, 
                                                 Converter={StaticResource ConvertSomething}, 
                                                 ConverterParameter={StaticResource Something}}" 
                                                 Source={StaticResource bindingProxy}}" />
    </DataGrid.Columns>
</DataGrid>
XAML

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get 
        {
            return (object)GetValue(DataProperty); 
        }

        set
        {
            SetValue(DataProperty, value);
        }
    }

    public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data",
                                                                                typeof(object),
                                                                                typeof(BindingProxy));
}
<DataGrid>
    <DataGrid.Resources>
        <local:BindingProxy x:Key="bindingProxy" Data="{Binding}" />
    </DataGrid.Resources>

    <DataGrid.Columns>
        <DataGridTextColumn Visibility="{Binding Path=MySelectedItem, 
                                                 Converter={StaticResource ConvertSomething}, 
                                                 ConverterParameter={StaticResource Something}}" 
                                                 Source={StaticResource bindingProxy}}" />
    </DataGrid.Columns>
</DataGrid>


你能发布完整的代码吗?@NullReferenceException我希望这足够了。你使用的是什么版本的
VisaulStudio
?2013 Premium with Update 1如果我没有弄错的话,它将不起作用,因为
DataGridColumns
不在
DataGrid
的可视化树中。