将字符串列表绑定到DataGridTemplateColumn TextBox [英] Binding a string list to a DataGridTemplateColumn TextBox

查看:114
本文介绍了将字符串列表绑定到DataGridTemplateColumn TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试这样做,则会收到 System.Windows.Markup.XamlParseException

If I try to do that, I get "System.Windows.Markup.XamlParseException".

我的XAML代码如下所示:

My XAML code looks like this:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid ItemsSource="{Binding ErrorLog}" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False">
            <DataGrid.Columns>
                <!-- This is working -->
                <DataGridTextColumn Binding="{Binding}" Header="Fehler" Width="*"/>

                <!-- This is not working -->
                <DataGridTemplateColumn Header="">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding}" AcceptsReturn="True" AcceptsTab="True" TextWrapping="Wrap"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

我的代码背后的代码如下:

My code behind code looks like this:

using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public ObservableCollection<string> ErrorLog { get; set; } = new ObservableCollection<string>()
        {
            "A",
            "B"
        };
    }
}


推荐答案

其他异常中的信息是:双向绑定需要Path或XPath。
TextBox文本属性默认情况下具有TwoWay绑定模式。 TwoWay绑定不接受像 {Binding} 这样的空绑定。请尝试以下操作。

Additional information from exception is: Two-way binding requires Path or XPath. TextBox Text property has TwoWay binding mode by default. TwoWay bindings do not accept empty bindings like "{Binding}". Try the following.

<DataGridTemplateColumn Header="">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=.}" AcceptsReturn="True" AcceptsTab="True" TextWrapping="Wrap"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我认为,更改收集类型并使用某些自定义类型代替字符串将是更好的解决方案:
XAML:

I think, changing your collection type and using some custom type instead of string will be a better solution though: XAML:

<Grid>
    <DataGrid ItemsSource="{Binding ErrorLog}" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Message}" Header="Fehler" Width="*"/>
            <DataGridTemplateColumn Header="">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Message}" AcceptsReturn="True" AcceptsTab="True" TextWrapping="Wrap"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }
}

public class MainViewModel
{
    public ObservableCollection<Error> ErrorLog { get; set; } = new ObservableCollection<Error>
                                                                    {
                                                                        new Error("A"),
                                                                        new Error("B"),
                                                                    };
}

public class Error
{
    public Error(string message)
    {
        Message = message;
    }

    public string Message { get; set; }
}

还考虑实现INotifyPropertyChanged接口,以便能够在视图模型中更改消息需要。

Also consider implement INotifyPropertyChanged interface to be able to change message from view model if needed.

这篇关于将字符串列表绑定到DataGridTemplateColumn TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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