将用户控制可见性绑定到valueconverter

时间:2014-07-16 10:39:53

标签: c# c#-4.0 wpf-controls

我有一个ValueConverter可以在元素上正常工作,例如像STACKPANEL。方法User.OwnsRight(Int32)返回true或false。

[ValueConversion(typeof(object), typeof(System.Windows.Visibility))]
public class ConverterUserRightVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bxSuite.Base.Objects.User locUser = (bxSuite.Base.Objects.User)value;

        if (parameter == null) return System.Windows.Visibility.Visible;
        if (locUser == null) return System.Windows.Visibility.Visible;

        if (locUser.OwnsRight(System.Convert.ToInt32(parameter)))
        {
            return System.Windows.Visibility.Visible;
        }
        else
        {
            return System.Windows.Visibility.Collapsed;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Windows.Visibility locVisibility = (System.Windows.Visibility)value;
        if (locVisibility == System.Windows.Visibility.Hidden || locVisibility == System.Windows.Visibility.Collapsed)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

所以这个XAML效果很好:

<StackPanel Visibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=4}"></StackPanel>

但是,如果我将我的一个UserControls的可见性绑定到此转换器,转换器甚至不会被解雇。 UserControl本身工作正常。

<my:MenuButtonLarge Visibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=4}"/>

任何想法为什么?任何帮助赞赏!非常感谢。

编辑:我尝试过另一个转换器,只需在Label控件上设置文本即可。工作正常,但如果我在usercontrol的Textproperty(依赖属性)上使用它,转换器与ConverterParameter不会被解雇?我错过了什么?

0 个答案:

没有答案