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

时间:2014-04-08 16:55:58

标签: c# wpf data-binding

有人可以解释为什么以下示例中的数据绑定不起作用以及我需要做些什么来使其工作?我整天搜索了类似的例子并阅读了很多关于数据绑定的文章,但是我找不到解释。

<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')

非常感谢!

1 个答案:

答案 0 :(得分:1)

正如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();
    }
}

<强>视图模型

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
}