微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当 itemSource 绑定到模型 (\

如何解决当 itemSource 绑定到模型 (\

我有一个包含 ListView 的 UserControl(视图)。 itemsSource 绑定到 Model Character.cs。我将 ListView.ItemTemplate 设置为另一个视图,在该视图中,该视图是 3 个字符串的位置和一个用于选择字符的按钮。这三个字符串需要填充来自模型的数据(已完成),但按钮的命令需要绑定到 viewmodel 中的 ICommand。我无法访问此命令,因为 itemsSource 绑定到一个字符,它将带有按钮的视图的 dataContext 设置为一个字符。

这是具有 ListView 的 UserControl(视图)

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

这是具有 3 个字符串和一个按钮的视图

<Grid Background="{StadticResource BackGroundColor}">
    <Grid.ColumnDeFinitions>
        <ColumnDeFinition/>
        <ColumnDeFinition/>
        <ColumnDeFinition/>
        <ColumnDeFinition/>
    </Grid.ColumnDeFinitions>

    <Border Grid.Column="0" BorderBrush="white" BorderThickness="0,3,0"/>
    <Border Grid.Column="1" BorderBrush="white" BorderThickness="0,0"/>
    <Border Grid.Column="2" BorderBrush="white" BorderThickness="0,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>

这是带有 3 个按钮的 View 的 viewmodel

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);
    }

}

我希望按钮使用 CharacterItemviewmodel 类中的 SelectCharacterCommand。您如何建议我更改代码以实现这一目标,同时仍然坚持 MVVM 原则? 谢谢!

解决方法

好的,我知道了!昨天挣扎了几个小时然后今天寻求帮助......然后在 5 分钟后想通了:/

将 ItemsSource 绑定到 ObserbableCollection。这是在 LoadCharacters() 方法中。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。