Skip to main content
 首页 » 编程设计

c#之我的 ItemsControl 和数据绑定(bind)有什么问题

2024年02月24日29lhb25

我正在修改我的简单十六进制编辑器,以练习使用我最近学到的有关 WPF 数据绑定(bind)的知识。我不确定我在这里做错了什么。

据我了解,对于集合“后端”(继承自 ObservableCollection)中的每个字节,我的 ItemsControl 应该在资源下应用 DataTemplate。此模板只是一个绑定(bind)到值转换器的文本框。所以我希望看到一行文本框,每个文本框都包含一个字节的字符串表示形式。当我使用此 XAML 时,我得到的只是一行不可编辑的文本,据我所知,它没有使用文本框。我做错了什么?

我在下面粘贴了我的 XAML,删除了不相关的部分(菜单声明、架构等)。

<Window ...> 
        <Window.Resources> 
            <local:Backend x:Key="backend" /> 
            <local:ByteConverter x:Key="byteConverter" /> 
            <DataTemplate DataType="byte"> 
                <TextBox Text="{Binding Converter={StaticResource byteConverter}}" />                     
            </DataTemplate> 
        </Window.Resources> 
        <StackPanel> 
            <ItemsControl ItemsSource="{Binding Source={StaticResource backend}}"> 
                <ItemsControl.ItemsPanel> 
                    <ItemsPanelTemplate> 
                        <WrapPanel /> 
                    </ItemsPanelTemplate> 
                </ItemsControl.ItemsPanel> 
            </ItemsControl> 
        </StackPanel> 
    </Window> 

请您参考如下方法:

当您在默认命名空间之外使用类型时,您想绕过类型名称的默认值转换器。您还想使用 GetType() 返回的类型名称,而不是 C# 编译器使用的类型名称。

首先,确保您已经声明了引用 System 命名空间的命名空间前缀,例如:

xmlns:sys="clr-namespace:System;assembly=mscorlib" 

在您的 DataTemplate 中,使用 Type 标记扩展引用类型:

DataType="{x:Type sys:Byte}" 

编辑

这是一个最小的工作示例:

<Window x:Class="ByteTemplateDemo.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:ByteTemplateDemo="clr-namespace:ByteTemplateDemo" Title="MainWindow" Height="350" Width="525"> 
    <DockPanel> 
        <DockPanel.Resources> 
            <ByteTemplateDemo:ByteConverter x:Key="ByteConverter"/> 
            <DataTemplate DataType="{x:Type sys:Byte}"> 
                <TextBox Foreground="Red" Text="{Binding Path=., Converter={StaticResource ByteConverter}}"/> 
            </DataTemplate> 
        </DockPanel.Resources> 
        <ItemsControl x:Name="Items" ItemsSource="{Binding}"/> 
    </DockPanel> 
</Window> 

值转换器:

public class ByteConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        Byte b = (Byte)value; 
        return "b" + b.ToString(); 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        string strValue = value as string; 
        Byte result; 
        if (Byte.TryParse(strValue, out result)) 
        { 
            return result; 
        } 
        return DependencyProperty.UnsetValue; 
    } 
} 

在代码隐藏中:

public MainWindow() 
{ 
    InitializeComponent(); 
    ObservableCollection<byte> bytes = new ObservableCollection<byte>(); 
    bytes.Add(11); 
    bytes.Add(12); 
    bytes.Add(13); 
    bytes.Add(14); 
    Items.DataContext = bytes; 
} 

这表明模板和值转换器都在使用(因为您会在屏幕上看到带有以“b”开头的红色值的文本框)。

请注意,双向绑定(bind)在这种特定情况下不起作用,因为双向绑定(bind)需要属性名称。为了进行双向绑定(bind),您需要创建一个公开类型为 Byte 的命名属性的类,并绑定(bind)到这些对象的可观察集合。