簡體   English   中英

ListView中控件屬性的C#WPF數據綁定

[英]C# WPF Databinding of control properties in a ListView

有人可以解釋一下以下示例中的數據綁定為什么不起作用的原因,以及我需要做些什么才能使其起作用? 我整天都在搜索類似的示例,並閱讀了許多有關數據綁定的文章,但是我找不到對此的解釋。

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

非常感謝!

正如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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM