gpt4 book ai didi

c# - 主细节 MVVM WPF 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:52 25 4
gpt4 key购买 nike

我无法让我的绑定(bind)在 Detail ListView 上工作。我在下面粘贴了我所有的 MVVM 模式代码。请帮忙!!!

我的观点 :
DirectoryDe​​tailView.cs

<UserControl x:Class="S2.Views.DirectoryDetailView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding Path = DirectoryDetails}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedDirName, Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path = FileName}"
Header="File Name"/>
</GridView>
</ListView.View>
</ListView>
<ListView Grid.Column="1" Margin="10,0,0,0" ItemsSource="{Binding Path = DirectoryDetails}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path = FileDetails.Length}"
Header="Length"/>
<GridViewColumn DisplayMemberBinding="{Binding Path = FileDetails.LastAccessTime}"
Header="LastAccessTime"/>
</GridView>
</ListView.View>
</ListView>
</Grid>

我的模型:
public class DirectoryModel : INotifyPropertyChanged
{
private string _fileName;
private DateTime _createdTime;

public string FileName
{
get
{
return _fileName;
}
set
{
_fileName = value;
RaisePropertyChanged("FileName");
}
}

private IEnumerable<FileDetails> _fileDetails;

public IEnumerable<FileDetails> FileDetails
{
get
{
return _fileDetails;
}
set
{
_fileDetails = value;
RaisePropertyChanged("FileDetails");
}

}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion

protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = PropertyChanged;

if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

public class FileDetails
{
public long Length { get; set; }

public DateTime LastAccessTime { get; set; }
}

我的 View 模型:
public class DirectoryViewModel : BaseViewModel
{
private IEnumerable<DirectoryModel> _directoryDetails;

public IEnumerable<DirectoryModel> DirectoryDetails
{
get
{
var service = GetService<IDirectoryService>();
_directoryDetails = service.GetDirectoryDetails();
return _directoryDetails;
}
set
{
if(_directoryDetails != value)
{
_directoryDetails = value;
base.RaisePropertyChanged("DirectoryDetails");
}
}
}

private DirectoryModel _selectedDirName;

public DirectoryModel SelectedDirName
{
get
{
return _selectedDirName;
}
set
{
_selectedDirName = value;
base.RaisePropertyChanged("SelectedDirName");
}
}
}

请让我知道,我做错了什么?

谢谢,
股份公司

最佳答案

我不记得我是从哪里得到这种技术的,但它在用于调试绑定(bind)时非常有用

在项目中添加一个名为 Debugconverter 的类

public class DebugConverter : IValueConverter {
public object Convert(object value,
Type targetType, object parameter,
System.Globalization.CultureInfo culture) {

return value; //set the breakpoint here
}

public object ConvertBack(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture) {

return value;
}

}

然后我在 app.xaml 中添加对它的引用
     <currentProjectNamespace:DebugConverter
x:Key="debugConverter" />

然后在绑定(bind)中使用它,
Binding="{Binding Path=PropertyName, Converter={StaticResource debugConverter}}"

当绑定(bind)发生时,你会遇到断点,没有它我会被搞砸的。还要检查输出窗口,那里有绑定(bind)失败的列表。

关于c# - 主细节 MVVM WPF 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1566060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com