当X:参考WPF中解决,为什么XAML元素的顺序有影响吗? [英] When is x:Reference in WPF resolved and why does XAML element order affect it?

查看:232
本文介绍了当X:参考WPF中解决,为什么XAML元素的顺序有影响吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

X:我以后参考解决不了的重新排列XAML元素

下面我present工作code。正是这样说到按钮元素后的菜单项中的ContextMenu和MultiBinding在Button.IsEnabled绑定成为移动破碎DataGrid的元素。在Button.IsEnabled只有MultiBinding坏了。它可以与注释块和X来代替:参考文献在单一的结合

两者扔XamlParseException。


  • 菜单项给System.Xaml.XamlObjectWriterException和有关未解决的参考消息会谈。

  • MultiBinding给System.Collections.Generic.KeyNotFoundException作为内部异常。

所以,当是X:?参考实际解析及为什么只有一些绑定时引用的元素来引用它的元素之后突破

下面是我的XAML:

<窗​​口x:类=WpfApplication1.MainWindow
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        的xmlns:XXX =CLR的命名空间:WpfApplication1
        标题=主窗口SizeToContent =WidthAndHeight>
    < Window.Resources>
        < XXX:BoolToVisibleConverter X:键=boolToVisibleConv>< / XXX:BoolToVisibleConverter>
        < XXX:NullToFalseConverter X:键=nullToFalseConv>< / XXX:NullToFalseConverter>
        < XXX:NullsOrToFalseConverter X:键=nullsOrToFalseConv>< / XXX:NullsOrToFalseConverter>
        <文本菜单X:键=利用这项>
            <菜单项
                标题=菜单项启用当行选择了
                IsEnabled ={结合
                    路径=的SelectedItem,
                    来源= {X:参考dataGridElement},
                    转换器= {StaticResource的nullToFalseConv}}/>
        < /文本菜单>
    < /Window.Resources>
    <&StackPanel的GT;
        <数据网格
            NAME =dataGridElement
            IsReadOnly =真/>
        <按钮
            CONTENT =按钮
            文本菜单={StaticResource的利用这项}
            能见度={结合
                路径= IsReadOnly,
                来源= {X:参考dataGridElement},
                转换器= {StaticResource的boolToVisibleConv}}>
            < Button.IsEnabled>
                <! - <绑定
                    PATH =的SelectedItem
                    来源={X:参考dataGridElement}
                    转换器={StaticResource的nullToFalseConv}/> - >
                < MultiBinding
                    转换器={StaticResource的nullsOrToFalseConv}>
                    <绑定
                        PATH =的SelectedItem
                        来源={X:参考dataGridElement}/>
                    <绑定
                        PATH =的SelectedItem
                        来源={X:参考dataGridElement}/>
                < / MultiBinding>
            < /Button.IsEnabled>
        < /按钮>
    < / StackPanel的>
< /窗GT;

下面是我的code的后面(无usings):

命名空间WpfApplication1
{
    ///<总结>
    ///为MainWindow.xaml交互逻辑
    ///< /总结>
    公共部分类主窗口:窗口
    {
        公共主窗口()
        {
            的InitializeComponent();
        }
    }
    公共类BoolToVisibleConverter:的IValueConverter
    {
        公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            如果(价值== NULL ||(布尔)值== FALSE)
                返回System.Windows.Visibility.Hidden;
            其他
                返回System.Windows.Visibility.Visible;
        }        公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            抛出新NotImplementedException();
        }
    }
    公共类NullsOrToFalseConverter:IMultiValueConverter
    {
        公共对象转换(对象[]值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            的foreach(在值对象VAL)
            {
                如果(VAL == NULL)
                    返回false;
            }
            返回true;
        }        公用对象[] ConvertBack(对象的值,类型[] TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            抛出新NotImplementedException();
        }
    }
    公共类NullToFalseConverter:的IValueConverter
    {
        公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            收益率(值!= NULL);
        }        公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
        {
            抛出新NotImplementedException();
        }
    }
}


解决方案

从MSDN摘录(的 http://msdn.microsoft.com/en-us/library/ee795380.aspx )。

X:参考是在2009年XAML定义在WPF一个结构,你可以使用XAML 2009年的功能,但仅限于XAML不是WPF标记编译。标记编译XAML和XAML的BAML形式当前不支持XAML中2009年语言的关键字和功能。

x:Reference can not be resolved after I re-arrange elements in XAML.

Here I present a working code. Just move the DataGrid element so it comes after the button element and the bindings for the MenuItem in ContextMenu and MultiBinding in Button.IsEnabled become broken. In Button.IsEnabled only MultiBinding is broken. It can be replaced with commented block and x:Reference works in that single binding.

Both throw XamlParseException.

  • MenuItem gives System.Xaml.XamlObjectWriterException and message talks about unresolved reference.
  • MultiBinding gives System.Collections.Generic.KeyNotFoundException as inner exception.

So when is that x:Reference actually resolved and why does only some bindings break when referenced element comes after the element that references it?

Here is my 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>

Here is my Code behind (without usings):

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();
        }
    }
}

解决方案

Excerpted from MSDN(http://msdn.microsoft.com/en-us/library/ee795380.aspx).

x:Reference is a construct defined in XAML 2009. In WPF, you can use XAML 2009 features, but only for XAML that is not WPF markup-compiled. Markup-compiled XAML and the BAML form of XAML do not currently support the XAML 2009 language keywords and features.

这篇关于当X:参考WPF中解决,为什么XAML元素的顺序有影响吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆