简体   繁体   中英

can wpf window visibility be bound with a static resource converter 'introduced' later?

I have:

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

}

And I get:

myexe.exe Error: 0 : [mynamespace.MyApp] Unhandled exception in published view 'App.FloatingToolbarWindow'. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '8' and line position '14'. ---> System.Exception: Cannot find resource named 'itemcounttovisibility'. Resource names are case sensitive.

This worked without problem before adding window visibility binding so the other converter was found and the window's content is bound to the same GuiItemsInstance.FloatingToolbarsView without any problem.

You can do it by setting the property below resource declaration:

<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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM