使用值转换器绑定到通用应用程序中的“可见性”

时间:2015-04-12 01:32:09

标签: windows-store-apps win-universal-app resourcedictionary ivalueconverter

接下来,我们需要创建一个转换器来处理bool值和Visibility。这是:

   namespace DictionaryApp.Model
{
    public class VisiableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }

        public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
        {
            bool visibility = (bool)value;
            return visibility ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;
            return (visibility == Visibility.Visible);
        }
    }
}

接下来,在UserControl的资源中添加转换器。在我的简单项目中,我必须创建此部分 - 但希望您已经在真实的应用程序中使用它了!

您可能还需要在page.xaml中为项目的命名空间添加xmlns属性。

x:Class="DictionaryApp.PageDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DictionaryApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ms="using:DictionaryApp.Model"
mc:Ignorable="d">
<UserControl.Resources>
    <ms:VisiableConverter x:Key="VisiableConverter"></ms:VisiableConverter>
</UserControl.Resources>

名称“XYZ”在名称空间“axy

中不存在

0 个答案:

没有答案