Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将修改后的属性绑定到控件Windows Universal Apps(XAML/C#)_C#_Windows_Xaml - Fatal编程技术网

如何将修改后的属性绑定到控件Windows Universal Apps(XAML/C#)

如何将修改后的属性绑定到控件Windows Universal Apps(XAML/C#),c#,windows,xaml,C#,Windows,Xaml,我正在开发一个windows通用应用程序,并试图解决数据绑定问题 我有一个listview,它有一个项目模板和数据模板,其中绑定了自定义类的属性 <ListView> <ListView.ItemTemplate> <DataTemplate> <Textblock Text="{Binding Name}"/> </DataTemplate> </ListV

我正在开发一个windows通用应用程序,并试图解决数据绑定问题

我有一个listview,它有一个项目模板和数据模板,其中绑定了自定义类的属性

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Textblock Text="{Binding Name}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
然而,这看起来非常混乱,我想知道是否有一种更整洁的方法可以在不创建单独属性的情况下完成这项工作?

您可以使用

创建一个带有StringToUpper类的StringToUpper.cs文件,该StringToUpper类继承了以下格式:

public class StringToUpper: IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        var valueString = value.ToString();
        if (!string.IsNullOrEmpty(valueString))
        {
            return valueString.ToUpper();
        }

        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // do nothing.
    }
}
    ...   
    xmlns:converter="clr-namespace:StringToUpper"
    ...>
    <Window.Resources>
        <converter:StringToUpper x:Key="StringToUpperConverter" />
    </Window.Resources>
添加所创建转换器的资源:

public class StringToUpper: IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        var valueString = value.ToString();
        if (!string.IsNullOrEmpty(valueString))
        {
            return valueString.ToUpper();
        }

        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // do nothing.
    }
}
    ...   
    xmlns:converter="clr-namespace:StringToUpper"
    ...>
    <Window.Resources>
        <converter:StringToUpper x:Key="StringToUpperConverter" />
    </Window.Resources>
。。。
xmlns:converter=“clr命名空间:StringToUpper”
...>
添加转换器:

<Textblock Text="{Binding Name, Converter={StaticResource StringToUpperConverter}}"/>


这是一个关于WPF中转换器的好教程

您可以创建一个转换器,将属性的值转换为视图所需的形式(在您的例子中,是字符串的简单大写形式)

您的转换器类可能如下所示:

 public class StringToUpperStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var valueString = value.ToString();
            if (!string.IsNullOrEmpty(valueString))
            {
                return valueString.ToUpper();
            }

            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Textblock Text="{Binding Name, Converter={StaticResource StringToUpperStringConverter}}"/>
您的xaml可能在资源部分中定义了以下内容:

<converters:StringToUpperStringConverter x:Key="StringToUpperStringConverter" />

然后您的绑定将如下所示:

 public class StringToUpperStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var valueString = value.ToString();
            if (!string.IsNullOrEmpty(valueString))
            {
                return valueString.ToUpper();
            }

            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
<Textblock Text="{Binding Name, Converter={StaticResource StringToUpperStringConverter}}"/>