WPF转换器不起作用 [英] WPF Converter doesn't work

查看:320
本文介绍了WPF转换器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
我想检查某个数字是否大于另一个,并且不希望我做些明显的事情.
我想检查我的ListBox是否具有Items的确切情况.如果没有,我的加载更多"按钮将不会显示.

这是我的转换器,我很确定它是正确的:

Hello there!
I want to check if some number is greater than another and dispite that I want to make something visable or not.
It this exact case I want to check if my ListBox has Items. If not, my "Load more" button should not show up.

This is my converter, I''m pretty sure it is right written:

namespace NeonMika.EightTracksPlayer
{
    public class GreaterIntegerToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ( (int)value > (int)parameter )
                return Visibility.Visible;
            else
                return Visibility.Hidden;
        }

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

        #endregion
    }
}



这是我的具有可见性绑定到我的列表框的按钮(名为MixControl):



And this is my Button with the Visibility-Binding to my ListBox (named MixControl):

<Button Name="LoadMoreButton" HorizontalAlignment="Stretch" Click="LoadMoreButton_Click" Visibility="{Binding ElementName=MixControl, Path=Items/Count, Converter={StaticResource GreaterIntegerConverter}, ConverterParameter=0}">Load more</Button>



可以看到我的错误在哪里吗?
非常感谢您的帮助!

谢谢,Markus



Can you see where my error is?
I really appreciate your help!

Thanks, Markus

推荐答案

您的XML读为:

Your XML reads:

Visibility="{Binding ElementName=MixControl, Path=Items/Count, Converter={StaticResource GreaterIntegerConverter}, ConverterParameter=0}">



但是,您要调用的函数是:



Yet, the function that you want to call is:

NeonMika.EightTracksPlayer.GreaterIntegerToVisibilityConverter.Convert



所以在我看来,您的XML应该读为:



So it seems to me your XML should read something like:

Visibility="{Binding ElementName=MixControl, Path=Items/Count, Converter={StaticResource NeonMika.EightTracksPlayer.GreaterIntegerToVisibilityConverter.Convert}, ConverterParameter=0}">



但是,即使这似乎有点可疑,因为您已将GreaterIntegerToVisibilityConverter声明为一个类,并将Convert声明为该类的非静态成员.

因此,除非您在该XML行之前的某个位置实例化GreaterIntegerToVisibilityConverter,否则从任何地方都无法访问Covert成员函数.

而且,如果您要在某个地方实例化一个实例,则完全不清楚您在其他地方实例化的事物实际上是如何与XML解释器在试图评估可见性时所知道的事物绑定在一起的.

在您的convert函数上设置一个断点,然后在调试器中运行它-我很确定它永远不会被调用.

更新:

假设您的XAML前面有类似的内容:



But even that seems a bit dubious, since you''ve declared GreaterIntegerToVisibilityConverter as a class and Convert as a non-static member of it.

So unless you are instantiating a GreaterIntegerToVisibilityConverter somewhere prior to that line of XML the Covert member function isn''t accessible from anywhere.

And if you are instantiating one somewhere, it''s not at all clear how the thing you instantiated elsewhere is actually bound to something that is known by the XML interpreter at the point it tries to evaluate Visibility.

Set a break point on your convert function and run it in the debugger -- I''m pretty sure it never gets called.

UPDATE:

Assuming you have something like this earlier in your XAML:

<local:NeonMika.EightTracksPlayer.GreaterIntegerToVisibilityConverter x:Key="GreaterIntegerConverter"/>



那么您应该绑定到正确的转换器函数-如果是这种情况,则问题出在元素或路径上.

假设元素拼写正确,那么路径可能存在问题.

尝试将路径更改为"Items",然后让转换器从集合中提取计数.



then you should be binding to the correct converter function -- if that''s the case, the problem lies either with the element or the path.

Assuming the element is spelled right, then maybe there is an issue with the Path.

Try changing the path to just "Items" and have your converter extract the count from the collection.


好,我现在可以正常使用了.
我现在不解释为什么Visual Studio绑定助手(通过使用属性列表)使用"/"构建指向属性的路径,但是它必须是.".
提供更多信息:如果您使用ConvertParameter(像我一样),则此参数应放在"abc"中.这样可以确保将参数识别为字符串.但是您也可以在此处使用其他绑定(如果您的参数取决于其他属性).

现在我的工作代码如下:

在Window.Resources(或App.xaml等)中声明:
Ok, I got it working now.
I don''t now why the Visual Studio Binding Assistent (by using the Properties List) builds the Path to the Property with "/", but it has to be a "."
To give a little more information: If you use a ConvertParameter (like me), this parameter should be put in ''abc''. This ensures the parameter is recognized as string. But you can also use another binding here (if your parameter is depending on another Property).

Now my working code is the following:

Declaration in the Window.Resources (or App.xaml, etc.):
<local:greaterintegertovisibilityconverter x:key="GreaterIntegerConverter" xmlns:x="#unknown" xmlns:local="#unknown"></local:greaterintegertovisibilityconverter>



然后将其用作控件:



Then this for the control:

<button name="LoadMoreButton" horizontalalignment="Stretch" click="LoadMoreButton_Click" visibility="{Binding ElementName=MixControl, Path=Items.Count, Converter={StaticResource GreaterIntegerConverter}, ConverterParameter=''0''}">Load more</button>



这是转换器的工作代码:



And this is the working code for the converter:

public class GreaterIntegerToVisibilityConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            int val = System.Convert.ToInt32(value);
            int para = System.Convert.ToInt32(parameter);

            if ( val > para )
                return Visibility.Visible;
            else
                return Visibility.Hidden;
        }
        catch ( Exception ex )
        {
            return Visibility.Hidden;
        }
    }

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



谢谢您的想法!



Thanks for your ideas!


这篇关于WPF转换器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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