什么时候是x:WPF中的引用已解决,为什么XAML元素顺序会影响它?

use*_*138 10 wpf binding reference xamlparseexception

x:在XAML中重新排列元素后,无法解析引用.

在这里,我提出一个工作代码.只需移动DataGrid元素,使其位于按钮元素之后,并且ButtonMenu中的MenuItem和Button.IsEnabled中的MultiBinding的绑定将被破坏.在Button.IsEnabled中,只有MultiBinding被破坏.它可以用注释块和x替换:参考在单个绑定中起作用.

两者都抛出XamlParseException.

  • MenuItem提供System.Xaml.XamlObjectWriterException和有关未解析引用的消息.
  • MultiBinding将System.Collections.Generic.KeyNotFoundException作为内部异常.

那么什么时候x:引用实际上已经解决了,为什么只有一些绑定在引用元素位于引用它的元素之后才会中断?

这是我的XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xxx="clr-namespace:WpfApplication1"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Window.Resources>
        <xxx:BoolToVisibleConverter x:Key="boolToVisibleConv"></xxx:BoolToVisibleConverter>
        <xxx:NullToFalseConverter x:Key="nullToFalseConv"></xxx:NullToFalseConverter>
        <xxx:NullsOrToFalseConverter x:Key="nullsOrToFalseConv"></xxx:NullsOrToFalseConverter>
        <ContextMenu x:Key="MyMenu">
            <MenuItem 
                Header="Menuitem enabled when row selected" 
                IsEnabled="{Binding 
                    Path=SelectedItem, 
                    Source={x:Reference dataGridElement}, 
                    Converter={StaticResource nullToFalseConv}}" />
        </ContextMenu>
    </Window.Resources>
    <StackPanel>
        <DataGrid 
            Name="dataGridElement" 
            IsReadOnly="True" />
        <Button 
            Content="Button" 
            ContextMenu="{StaticResource MyMenu}" 
            Visibility="{Binding 
                Path=IsReadOnly, 
                Source={x:Reference dataGridElement},
                Converter={StaticResource boolToVisibleConv}}">
            <Button.IsEnabled>
                <!--<Binding 
                    Path="SelectedItem" 
                    Source="{x:Reference dataGridElement}" 
                    Converter="{StaticResource nullToFalseConv}"/>-->
                <MultiBinding 
                    Converter="{StaticResource nullsOrToFalseConv}">
                    <Binding 
                        Path="SelectedItem" 
                        Source="{x:Reference dataGridElement}"/>
                    <Binding 
                        Path="SelectedItem" 
                        Source="{x:Reference dataGridElement}"/>
                </MultiBinding>
            </Button.IsEnabled>
        </Button>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

这是我的代码背后(没有使用):

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class BoolToVisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || (bool)value == false)
                return System.Windows.Visibility.Hidden;
            else
                return System.Windows.Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class NullsOrToFalseConverter : IMultiValueConverter
    {
        public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            foreach (object val in value)
            {
                if (val == null)
                    return false;
            }
            return true;
        }

        public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class NullToFalseConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value != null);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Céd*_*non -1

x:Reference在 WPF 中必须避免。因为此标记扩展是 XAML 语言 (2009) 最近添加的内容。而且WPF中并不完全支持它。ElementName在您的中使用Binding而不是x:Reference.

<Binding Path="SelectedItem" 
         ElementName="dataGridElement"/>
Run Code Online (Sandbox Code Playgroud)

在MSDN上。

  • @Cédric Bignon,在某些特定情况下 _ElementName_ 不起作用,而 _{x:Reference}_ 是 .NET 4 或更高版本的绝佳解决方法。具体来说,当尝试从 _DataGridTemplateColumn_ 内进行绑定时,由于 WPF 树中的问题,_ElementName_ 会失败,尽管根据我的经验,它只影响 Windows XP。请参阅[这个 stackoverflow 问题](http://stackoverflow.com/questions/19244111/what-is-the-difference- Between-xreference-and-elementname) 以及[这个](http://stackoverflow.com/问题/5834336/wpf-combobox-binding-problem-in-datatemplate) (3认同)