gpt4 book ai didi

c# - 如何使用 Caliburn.Micro 将容器绑定(bind)到 MVVM 标准中的 ListView 控件?

转载 作者:行者123 更新时间:2023-11-30 16:50:31 24 4
gpt4 key购买 nike

我正在用 MVVM 编写程序(C#) 和 XAML使用 Caliburn.Micro图书馆。

我想知道如何将我的 Listview 控件与一些容器(例如 List<Type>)绑定(bind), IEnumerable<Type>ObservableCollection< Type>

Views\MainView.xaml

<ListView Grid.Column="1" x:Name="FileListView" ItemsSource="{Binding ListOfFile}" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="10" Background="#FFE6EEF7">
<ListView.View>
<GridView>
<GridViewColumn Header="Status" Width="100" />
<GridViewColumn Header="Name" Width="85" />
<GridViewColumn Header="Size" Width="100" />
<GridViewColumn Header="System Type" Width="100" />
<GridViewColumn Header="Email Count" Width="100" />
<GridViewColumn Header="Info Count" Width="100" />
</GridView>
</ListView.View>
</ListView>

ViewModels\MainViewModel.cs

namespace ListBox_CaliburnMicro
{
public class MainViewModel : Screen
{
// ...

public MainViewModel()
{

}

public class FileItem
{
public string FileStatus { get; private set; }
public string FileName { get; private set; }
public string FileSize { get; private set; }
public string FileType { get; private set; }
public string FileEmailCount { get; private set; }
public string FileInfoCount { get; private set; }

public FileItem(string s1 = "", string s2 = "", string s3 = "", string s4 = "", string s5 = "", string s6 = "")
{
FileStatus = s1;
FileName = s2;
FileSize = s3;
FileType = s4;
FileEmailCount = s5;
FileInfoCount = s6;
}
}
}
}

最佳答案

首先,您应该在 XAML 中为您的属性编写标记。让我们想象一下你有:

型号:

public class Person
{
public int IdPerson { get; set; }
public string Name { get; set; }
public string SurName { get; set; }
}

View 模型:

public class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
FillData();
}

private void FillData()
{
persons = new ObservableCollection<Person>();
for (int i = 0; i < 30; i++)
{
persons.Add(new Person() { IdPerson = i, Name = "Ben & Joseph " + i.ToString(), SurName = "Albahari" });
}
}

private ObservableCollection<Person> persons;

public ObservableCollection<Person> Persons
{
get { return persons; }
set { persons = value;
}
}


public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

更新:

XAML:

<Window x:Class="SOWpfApplication.MainWindow"
...the code omitted for the brevirt...
xmlns:vm="clr-namespace:SOWpfApplication.ViewModel"
Title="MainWindow" Height="750" Width="925" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<ListView ItemsSource="{Binding Path=Persons}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="Auto"
DisplayMemberBinding="{Binding IdPerson}" >
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}"
Header="Name" Width="Auto"/>
<GridViewColumn DisplayMemberBinding="{Binding SurName}"
Header="Price" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
</Window>

关于c# - 如何使用 Caliburn.Micro 将容器绑定(bind)到 MVVM 标准中的 ListView 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34989744/

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