C# 当itemSource绑定到模型时,如何将视图中按钮的command属性设置为关联ViewModel中的ICommand(\
我有一个具有ListView的UserControl(视图)。itemsSource绑定到模型Character.cs。我将ListView.ItemTemplate设置为另一个视图,在该视图中,它是一个放置3个字符串的位置和一个用于选择字符的按钮。这三个字符串需要用模型中的数据填充(这已完成)但是按钮的命令需要绑定到ViewModel中的ICommand。我无法访问此命令,因为itemsSource绑定到一个字符,该字符将带有按钮的视图的dataContext设置为一个字符 这是具有ListView的UserControl(视图)C# 当itemSource绑定到模型时,如何将视图中按钮的command属性设置为关联ViewModel中的ICommand(\,c#,wpf,listview,datatemplate,itemssource,C#,Wpf,Listview,Datatemplate,Itemssource,我有一个具有ListView的UserControl(视图)。itemsSource绑定到模型Character.cs。我将ListView.ItemTemplate设置为另一个视图,在该视图中,它是一个放置3个字符串的位置和一个用于选择字符的按钮。这三个字符串需要用模型中的数据填充(这已完成)但是按钮的命令需要绑定到ViewModel中的ICommand。我无法访问此命令,因为itemsSource绑定到一个字符,该字符将带有按钮的视图的dataContext设置为一个字符 这是具有ListV
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--disable the selected item thingyyy??-->
<ListView ItemsSource="{Binding Characters}"
SelectedItem="{Binding CharacterItemVM.BoundCharacter}"
Background="{StaticResource BackGroundColorDark}"
HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<v:CharacterItemView Grid.Column="0"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
我希望按钮使用CharacterItemViewModel类中的SelectCharacterCommand。您建议我如何更改代码以实现这一点,同时仍然坚持MVVM原则?
谢谢!好的,我现在明白了!昨天挣扎了几个小时,今天又寻求帮助……5分钟后就明白了:/ 将ItemsSource绑定到ObserableCollection。这在LoadCharacters()方法中
公共类CharacterListViewModel:ObserveObject
{
私有ICharacterDataService数据服务;
私有只读字符库字符库;
私有CharacterItemViewModel characterItemVM;
public CharacterItemViewModel CharacterItemVM
{
获取{return characterItemVM;}
集合{onPropertyHaged(ref characterItemVM,value);}
}
//ObservableCollection是一个wpf友好列表
公共静态ObservableCollection字符{get;private set;}
公共可观测集合特征{get;private set;}
//调用此函数以加载UI中的字符
公共ICommand LoadCharactersCommand{get;private set;}
public CharacterListViewModel(MockDataService数据服务,CharacterStore\u CharacterStore)
{
characterItemVM=新的CharacterItemViewModel(characterStore,新字符(“,”,”);
characterStore=\u characterStore;
this.dataService=dataService;
LoadCharactersCommand=新的RelayCommand(LoadCharacters);
LoadCharacters();
}
公共void LoadCharacters()
{
//LoadCharacters(dataService.GetCharacters());
Characters=newobserveCollection(dataService.GetCharacters());
CharacterItems=新的ObservableCollection();
Character[]characterArray=Characters.ToArray();
对于(int i=0;i
<Grid Background="{StadticResource BackGroundColor}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="white" BorderThickness="0,0,3,0"/>
<Border Grid.Column="1" BorderBrush="white" BorderThickness="0,0,3,0"/>
<Border Grid.Column="2" BorderBrush="white" BorderThickness="0,0,3,0"/>
<Label FontSize="18" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"
Content="{Binding Name, FallbackValue=N/A}"/>
<Label FontSize="18" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"
Content="{Binding CharacterClass, FallbackValue=N/A}"/>
<Label FontSize="18" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"
Content="{Binding Level, FallbackValue=N/A}"/>
<Button Content="select" FontSize="18" Grid.Column="3" Background="DarkGray"
Foreground="white" BorderThickness="3" BorderBrush="LightCyan"
Margin="15" Command="{Binding SelectCharacterCommand}"/>
</Grid>
public class CharacterItemViewModel : ObservableObject
{
private Character boundCharacter;
public Character BoundCharacter
{
get { return boundCharacter; }
set { OnPropertyChaged(ref boundCharacter, value); }
}
ICommand SelectCharacterCommand;
//TODO: get the characterStore from BookVM
public CharacterItemViewModel(CharacterStore characterStore)
{
SelectCharacterCommand = new SelectCharacterCommand(characterStore, this,
boundCharacter);
}
}
public class CharacterListViewModel : ObservableObject
{
private ICharacterDataService dataService;
private readonly CharacterStore characterStore;
private CharacterItemViewModel characterItemVM;
public CharacterItemViewModel CharacterItemVM
{
get { return characterItemVM; }
set { OnPropertyChaged(ref characterItemVM, value); }
}
// ObservableCollection is a wpf friendly list
public static ObservableCollection<Character> Characters { get; private set; }
public ObservableCollection<CharacterItemViewModel> CharacterItems { get; private set; }
// call this to load the characters in the UI
public ICommand LoadCharactersCommand { get; private set; }
public CharacterListViewModel(MockDataService dataService, CharacterStore _characterStore)
{
characterItemVM = new CharacterItemViewModel(characterStore, new Character("", "", ""));
characterStore = _characterStore;
this.dataService = dataService;
LoadCharactersCommand = new RelayCommand(LoadCharacters);
LoadCharacters();
}
public void LoadCharacters()
{
//CharacterItemVM.LoadCharacters(dataService.GetCharacters());
Characters = new ObservableCollection<Character>(dataService.GetCharacters());
CharacterItems = new ObservableCollection<CharacterItemViewModel>();
Character[] characterArray = Characters.ToArray();
for(int i = 0; i < Characters.Count - 1; i++)
{
CharacterItems.Add(new CharacterItemViewModel(characterStore, characterArray[i]));
}
OnPropertyChaged("Characters");
}
} // class