-1

原始问题在这里。 绑定到 ListView

我已经解决了一个问题,现在可以看到列名。但是,我无法弄清楚绑定。

输出窗口的错误:

 System.Windows.Data Error: 4 : Cannot find source for binding with reference 
'ElementName=This'. BindingExpression:Path=LogView.LogEntries; DataItem=null; target 
element is 'ListView' (Name='LoggingListView'); target property is 'ItemsSource' (type     'IEnumerable')

我最近从 LogFileWindow.XAML 尝试的 XAML 片段。我可以发布更多内容,但尽量保持混乱:

   <ListView Name="LoggingListView" ItemsSource="{Binding ElementName=This,  Path=LogView.LogEntries}  ">
                        <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=Date}"></GridViewColumn>
                            <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=Time}"></GridViewColumn>
                            <GridViewColumn Header="Event" DisplayMemberBinding="{Binding Path=Event}"></GridViewColumn>
                        </GridView>
                        </ListView.View>
                    </ListView>

C# 视图模型:

public class LogEntryViewModel : INotifyPropertyChanged
{
   public LogEntryViewModel(LogFileEntry le)
    {
        _date = le.Date;
        _time = le.Time;
        _event = le.Event;
    }     
    #region Members
    private string _date;
    public string Date
    {
        get { return _date; }
        set {_date = value;
        RaisePropertyChanged("Date");           
        }
    }

    private string _time;
    public string Time
    {
        get { return _time; }
        set
        {
            _time = value;
            RaisePropertyChanged("Time");

        }
    }

    private string _event;
    public string Event
    {
        get { return _event; }
        set { _event = value;
        RaisePropertyChanged("Event");
        }
    }
    private LogFileEntry _le;
    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void RaisePropertyChanged(string propertyName)
    {
        // take a copy to prevent thread issues
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class LogViewModel :ObservableCollection<LogEntryViewModel>
{
       private ObservableCollection<LogEntryViewModel> _LogEntries;
    public ObservableCollection<LogEntryViewModel> LogEntries = new    
  ObservableCollection<LogEntryViewModel>();

}

我尝试使用它的部分类声明和代码隐藏:

public partial class LogFileWindow : Window
{
    public  LogViewModel LogView = new LogViewModel();

}

4

1 回答 1

1

Visual Studio 错误是不言而喻的:您正在尝试使用 ElementName 进行绑定并使用 Name="This" 搜索可视元素(假设在您的 XAML 可视树中定义)。没有这样的事情(显然,我无法确定,因为您没有发布完整的 XAML 树)。

如果要将可视元素属性绑定到同一元素中的另一个属性,请使用RelativeSource Self

于 2012-11-02T02:36:20.590 回答