可以将wpf窗口可见性绑定到静态资源转换器'介绍'以后?

时间:2015-12-28 13:20:00

标签: c# wpf data-binding

我有:

<Window x:Class="mynamespace.Menu.FloatingToolbarWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:xpui="clr-namespace:mynamespace.UI"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"
         Visibility="{Binding GuiItemsInstance.FloatingToolbarsView, Converter={StaticResource itemcounttovisibility}}"
         SizeToContent="WidthAndHeight"
         d:DesignHeight="300" d:DesignWidth="300">

<Window.Resources>
    <ContextMenu x:Key="ToolbarContextMenu">
        <MenuItem Header="Move to top" Click="MoveToTopClick" />
        <MenuItem Header="Move to left" Click="MoveToLeftClick"/>
    </ContextMenu>
    <xpui:MenuItemToToolbarConverter x:Key="menutotoolbarconverter" />
    <xpui:IEnumerableHasItemsToVisibilityConverter x:Key="itemcounttovisibility" />
</Window.Resources>


namespace mynamespace.UI
{
    public class IEnumerableHasItemsToVisibilityConverter : IValueConverter
   {
        public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
        {
            if (((IEnumerable)value).GetEnumerator().MoveNext())
               return Visibility.Visible;

            return Visibility.Collapsed;
        }


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

}

我得到了:

myexe.exe错误:0:[mynamespace.MyApp]已发布视图中的未处理异常&#39; App.FloatingToolbarWindow&#39;。 System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---&GT; System.Windows.Markup.XamlParseException:&#39;在System.Windows.StaticResourceExtension&#39;上提供值。抛出异常。&#39;行号&#39; 8&#39;和行位置&#39; 14&#39; ---&GT; System.Exception:找不到名为&#39; itemcounttovisibility&#39;的资源。资源名称区分大小写。

在添加窗口可见性绑定之前,这没有问题,因此找到了另一个转换器,并且窗口的内容被绑定到相同的GuiItemsInstance.FloatingToolbarsView而没有任何问题。

2 个答案:

答案 0 :(得分:2)

您可以通过在资源声明下设置属性来实现:

<Window>
    <Window.Resources>
        <ContextMenu x:Key="ToolbarContextMenu">
            <MenuItem Header="Move to top" Click="MoveToTopClick" />
            <MenuItem Header="Move to left" Click="MoveToLeftClick"/>
        </ContextMenu>
        <xpui:MenuItemToToolbarConverter x:Key="menutotoolbarconverter" />
        <xpui:IEnumerableHasItemsToVisibilityConverter x:Key="itemcounttovisibility" />
    </Window.Resources>
    <Window.Visibility>
        <Binding Mode="TwoWay" Path="GuiItemsInstance.FloatingToolbarsView" Converter="{StaticResource itemcounttovisibility}"/>
    </Window.Visibility>
</Window>

答案 1 :(得分:0)

关于上述nkoniishvt的答案: 必须在Mode =“ TwoWay”上设置绑定,才能调用转换器。

        <Binding Mode="TwoWay" Path="GuiItemsInstance.FloatingToolbarsView" Converter="{StaticResource itemcounttovisibility}"/>