在使用ItemsSource和帮助使用组合框之前,WPF项集合必须为空

本文关键字:集合 WPF ItemsSource 帮助 组合 | 更新日期: 2023-09-27 18:17:13

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1.5*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>  

    <Grid Grid.Column="0" Background="DarkCyan">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>    
            <ComboBox ItemsSource="{Binding Path=charList}" x:Name="comboBox" VerticalAlignment="Top" Width="Auto" Grid.ColumnSpan="2">
            <ComboBoxItem>
                <StackPanel Orientation="Horizontal">
                    <Image Source="Pictures'bagi_warrior.jpg" Width="100" Height="150"/>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="6*"/>
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Content="{Binding Path=Character.Name}" FontSize="30"/>
                        <Label Grid.Row="1" Content="{Binding Path=Character.Level}" FontSize="20"/>
                    </Grid>
                </StackPanel>
            </ComboBoxItem>
            <ComboBoxItem>
                <StackPanel Orientation="Horizontal">
                    <Image Source="Pictures'azure_knight.jpg" Width="100" Height="130"/>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="6*"/>
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Content="Azure Knight"  FontSize="30"/>
                        <Label Grid.Row="1" Content="Level 158"  FontSize="20"/>
                    </Grid>
                </StackPanel>
            </ComboBoxItem>
        </ComboBox>
        <Label x:Name="strLabel" Grid.Column="0" VerticalAlignment="Bottom" Height="30" Content="Str: " HorizontalContentAlignment="Center" FontSize="16"/>
        <Label x:Name="intLabel" Grid.Column="1" VerticalAlignment="Bottom" Height="30" Content="Int: " HorizontalContentAlignment="Center" FontSize="16"/>
        <Label x:Name="dexLabel" Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" Height="30" Content="Dex: " HorizontalContentAlignment="Center" FontSize="16"/>
        <Label x:Name="goldLabel" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Height="30" Content="Gold: " HorizontalContentAlignment="Center" FontSize="16"/>

    </Grid>
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="MyGrid" ItemsSource="{Binding Path=Item}"></DataGrid>
    </Grid>
</Grid>

我的XAML是这样的,它看起来像这样:

http://puu.sh/aLLrx.png

http://puu.sh/aLLsQ.jpg

我的代码是:
 public partial class MainWindow : Window
 {
    Character aloken;
    Character azureKnight;
    Character bagiWarrior;
    Character incarMagician;
    Character segitaHunter;
    Character segnale;
    Character viciousSummoner;
    private List<Character> _charList = new List<Character>();
    public MainWindow()
    {
        InitializeComponent();
        charList.Add(new Character("Bagi Warrior"));
        this.Item = new ObservableCollection<Item>();
        this.Character = new ObservableCollection<Character>();
        this.DataContext = this;
        //Character.Add(new Character(name: "Bagi Warrior", level: 197, characterClass: CharacterClass.Bagi_Warrior, gender: Gender.Male, strength: 450, intelligence: 4, dexterity: 84, gold: 147203352));
        //Character.Add(new Character(name: "Azure Knight", level: 158, characterClass: CharacterClass.Azure_Knight, gender: Gender.Male, strength: 390, intelligence: 120, dexterity: 92, gold: 204220567));
        //Character.Add(new Character(name: "Incar Magician", level: 169, characterClass: CharacterClass.Incar_Magician, gender: Gender.Female, strength: 4, intelligence: 512, dexterity: 57, gold: 172223520));
        //Character.Add(new Character(name: "Vicious Summoner", level: 203, characterClass: CharacterClass.Vicious_Summoner, gender: Gender.Male, strength: 423, intelligence: 89, dexterity: 45, gold: 114225587));
    }
    public List<Character> charList
    {
        get { return _charList; }
        set { _charList = value; }
    }

    private ObservableCollection<Character> _Character;
    public ObservableCollection<Character> Character
    {
        get { return _Character; }
        set { _Character = value; }
    }

    private ObservableCollection<Item> _Item;
    public ObservableCollection<Item> Item
    {
        get { return _Item; }
        set { _Item = value; }
    }
}

字符类(以防万一):http://pastebin.com/GFycKqDC

Item类(以防万一):http://pastebin.com/RgXzbFHk

这是一个RPG项目,你必须为一个组合框使用数据绑定,并为组合框创建一个ObservableCollection,并查看每个角色。

我遇到的问题是:在我的组合框中,我希望能够显示角色的"图像",旁边是他们的"级别"answers"名称"

但是我不希望它是静态的,我想创建一个字符列表,并将ItemsSource绑定到我的ComboBox,这样无论我选择哪个字符,基于该字符的信息都会更新。我该怎么做呢?

我被告知创建一个字符列表,然后为ComboBox Item创建一个数据模板,该模板包含绑定到selectedItem的pic,名称和级别,但无论我是在代码中做,还是在XAML中,它都会抛出一个异常,说集合必须是空的,然后我绑定到它。

我想将数据模板中的属性绑定到selectedItem,这样标签和信息就会适当地更新。

我做错了什么,为什么会抛出这个异常?

编辑

好了,现在问题解决了!然而,为什么我的标签绑定仍然不能为字符工作。姓名和角色。等级?

http://puu.sh/aLPr7.jpg

编辑2 公共主窗口(){InitializeComponent ();的性格。添加(新角色("Bagi Warrior"));的性格。添加(新角色("蔚蓝骑士"));这一点。Item = new ObservableCollection();这一点。

        this.DataContext = this;
        //Character.Add(new Character(name: "Bagi Warrior", level: 197, characterClass: CharacterClass.Bagi_Warrior, gender: Gender.Male, strength: 450, intelligence: 4, dexterity: 84, gold: 147203352));
        //Character.Add(new Character(name: "Azure Knight", level: 158, characterClass: CharacterClass.Azure_Knight, gender: Gender.Male, strength: 390, intelligence: 120, dexterity: 92, gold: 204220567));
        //Character.Add(new Character(name: "Incar Magician", level: 169, characterClass: CharacterClass.Incar_Magician, gender: Gender.Female, strength: 4, intelligence: 512, dexterity: 57, gold: 172223520));
        //Character.Add(new Character(name: "Vicious Summoner", level: 203, characterClass: CharacterClass.Vicious_Summoner, gender: Gender.Male, strength: 423, intelligence: 89, dexterity: 45, gold: 114225587));
    }
    //public List<Character> charList
    //{
    //    get { return _charList; }
    //    set { _charList = value; }
    //}

    private ObservableCollection<Character> _Character;
    public ObservableCollection<Character> Character
    {
        get { return _Character; }
        set { _Character = value; }
    }

和XAML:

<Grid Grid.Column="0" Background="DarkCyan">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox ItemsSource="{Binding Path=Character}" x:Name="comboBox" VerticalAlignment="Top" Width="Auto" Grid.ColumnSpan="2">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image x:Name="image" Source="{Binding Source={StaticResource ImageConverter}}" Width="100" Height="150"/>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition Height="6*"/>
                            </Grid.RowDefinitions>
                            <Label Grid.Row="0" Content="{Binding Path=Character.Name}" FontSize="30"/>
                            <Label Grid.Row="1" Content="{Binding Path=Character.Level}" FontSize="20"/>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>

在使用ItemsSource和帮助使用组合框之前,WPF项集合必须为空

您定义了ItemsSource两次。

使用

一次

ItemsSource="{Binding charList}"

,然后再加上

<ComboBoxItem>

您将希望用ComboBox.ItemTemplate替换ComboBoxItem元素,并为ComboxItem创建DataTemplate。然后,您将需要将Image Source绑定到Character类的属性上,或者使用ItemTemplateSelector让它为项目使用适当的DataTemplate。如果只有图像源位置在变化,您还可以使用绑定到定义字符类的属性的数据触发器。