プログラムが機能しない理由を理解するのに、恥ずかしいほど長い時間がかかりました。
基本的に、私が正しければ、キャラクタークラスは次のようになります。
public class Character
{
public string Name { get; set; }
public int Level { get; set; }
public int[] Agility { get; set; }
public int[] Strength { get; set; }
public int[] Stamina { get; set; }
public int[] Intelligence { get; set; }
}
Datagridがこれらの統計プロパティを互いに分離しているために機能しない理由。いくつかの異なるプロパティに基づいて行を作成する方法がわかりません。
Nathan Cooperが前に指摘したように、本当に必要なのは、これらの各プロパティをintとして格納できるStat(または同様のもの)と呼ばれる別のクラスです。次に、データグリッドをバインドするためにこれらのクラスのコレクションが必要です。
このように考えてください。現在、データは垂直方向に列にグループ化されています。データグリッドは、行にグループ化されるデータに使用されます。したがって、行を表すオブジェクトのコレクションが必要です。
時間をかけて、コンセプトが機能していることを示すサンプルアプリケーションを作成しました。
キャラクタークラスは次のようになります。
public class Character
{
public string Name { get; set; }
public int Level { get; set; }
public List<Stat> Stats { get; set; }
public Character()
{
}
public class Stat
{
public int Agility { get; set; }
public int Strength { get; set; }
public int Intelligence { get; set; }
public int Stamina { get; set; }
}
}
ここでは、ネイサンが話していたものと同様のStatクラスを作成したことがわかります。私が言ったように、このクラスはデータグリッドの「行」を格納するので、データグリッドが簡単に理解できる方法でデータをグループ化できます。
フォームのXAMLは次のようになります。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ComboBox Margin="5,5,5,5" DisplayMemberPath="Name" Grid.Row="0" ItemsSource="{Binding Path=Characters}" SelectedItem="{Binding Path=SelectedCharacter}"></ComboBox>
<DataGrid Grid.Row="1" Margin="5,5,5,5" AutoGenerateColumns="True" ItemsSource="{Binding Path=SelectedCharacter.Stats}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Agility" Binding="{Binding Path=Agility}"></DataGridTextColumn>
<DataGridTextColumn Header="Strength" Binding="{Binding Path=Strength}"></DataGridTextColumn>
<DataGridTextColumn Header="Intelligence" Binding="{Binding Path=Intelligence}"></DataGridTextColumn>
<DataGridTextColumn Header="Stamina" Binding="{Binding Path=Stamina}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
DataGridに表示する文字を選択するコンボボックスを作成しました。この文字の統計は、データグリッドの列にバインドされています。
最後に、ViewModelを示します(MVVMを使用しているかどうかはわかりませんが、そうする必要があります。とにかく、このコードは、ほとんど変更を加えることなく、コードビハインドに入れることができます)。
public class MainWindowViewModel:ViewModelBase
{
private IList<Character> _characters;
public IList<Character> Characters
{
get
{
return _characters;
}
set
{
_characters = value;
RaisePropertyChanged(()=>Characters);
}
}
private Character _character;
public Character SelectedCharacter
{
get
{
return _character;
}
set
{
_character = value;
RaisePropertyChanged(()=>SelectedCharacter);
}
}
public MainWindowViewModel()
{
InitializeCharacters();
}
private void InitializeCharacters()
{
Characters = new List<Character>();
SelectedCharacter = new Character();
Characters.Add(new Character
{
Name = "Tank",
Level = 3,
Stats = new List<Character.Stat>()
{
new Character.Stat()
{
Agility = 10,
Intelligence = 8,
Strength = 14,
Stamina = 16
},
new Character.Stat()
{
Agility = 11,
Intelligence = 9,
Strength = 16,
Stamina = 18
},
new Character.Stat()
{
Agility = 12,
Intelligence = 10,
Strength = 17,
Stamina = 20
}
}
});
Characters.Add(new Character
{
Name = "Healer",
Level = 4,
Stats = new List<Character.Stat>()
{
new Character.Stat()
{
Agility = 10,
Intelligence = 14,
Strength = 8,
Stamina = 10
},
new Character.Stat()
{
Agility = 11,
Intelligence = 16,
Strength = 9,
Stamina = 11
},
new Character.Stat()
{
Agility = 12,
Intelligence = 17,
Strength = 10,
Stamina = 13
},
new Character.Stat()
{
Agility = 14,
Intelligence = 20,
Strength = 10,
Stamina = 14
}
}
});
Characters.Add(new Character
{
Name = "Ranger",
Level = 6,
Stats = new List<Character.Stat>()
{
new Character.Stat()
{
Agility = 12,
Intelligence = 8,
Strength = 10,
Stamina = 8
},
new Character.Stat()
{
Agility = 14,
Intelligence = 9,
Strength = 11,
Stamina = 10
},
new Character.Stat()
{
Agility = 17,
Intelligence = 10,
Strength = 12,
Stamina = 11
},
new Character.Stat()
{
Agility = 18,
Intelligence =11,
Strength = 13,
Stamina = 12
},
new Character.Stat()
{
Agility = 20,
Intelligence = 12,
Strength = 15,
Stamina = 13
},
new Character.Stat()
{
Agility = 22,
Intelligence = 13,
Strength = 16,
Stamina = 13
}
}
});
}
}
ViewModelのスペースのほとんどは、ダミーデータを作成するために私が使用しているので、必要に応じて削除してください。重要な部分は、SelectedCharacterクラスとCharactersクラスの作成です。
これがプロジェクトへのリンクで、実際の動作を確認できます。(名前が苦手な名前を失礼します)
https://docs.google.com/open?id=0B9JOiSJxT9vjZVl1SHd3UzBiZEE
幸運を
U_U