ListView中控件属性的C#WPF数据绑定

本文关键字:C#WPF 数据绑定 属性 控件 ListView | 更新日期: 2023-09-27 17:57:37

有人能解释一下为什么下面例子中的数据绑定不起作用,以及我需要做些什么才能使它起作用吗?我已经搜索了一整天类似的例子,并阅读了许多关于数据绑定的文章,但我找不到对此的解释。

<Window x:Class="WpfApplicationTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListView Name="ListView1" Height="Auto" Width="Auto">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Col1" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="TextBox1" Text="blablabla" Width="150"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Col2" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="TextBox2" Text="{Binding ElementName=TextBox1, Path=Text}" Width="150"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListViewItem>test</ListViewItem>
    </ListView>
</Grid>

错误为:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=TextBox1'. BindingExpression:Path=Text; DataItem=null; target element is 'TextBox' (Name='TextBox2'); target property is 'Text' (type 'String')

非常感谢!

ListView中控件属性的C#WPF数据绑定

正如HighCore和kmacdonald所说,这是一个范围问题。无法从外部访问DataTemplate内部元素。MVVM是解决方案,因此您应该将共享文本放在ViewModel上。

窗口

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView Name="ListView1" Height="Auto" Width="Auto">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Col1" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="TextBox1" Text="{Binding Path=Text, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Width="150"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Col2" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Name="TextBox2" Text="{Binding Path=Text, Mode=OneWay}" IsReadOnly="True" Width="150"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
            <ListViewItem Content="{Binding}"/>
        </ListView>
    </Grid>
</Window>

后面的窗口代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

ViewModel

public class ViewModel: INotifyPropertyChanged
{
    private String text;
    public String Text
    {
        get
        {
            return this.text;
        }
        set
        {
            this.text = value;
            this.NotifyPropertyChanged("Text");
        }
    }
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}