初歩的な質問をしてください。実験として作成したアプリがあります (C#、XAML)。私のメイン ウィンドウには、2 つのテキスト ボックスがあります。最初のテキスト ボックスは、ユーザーが入力する値であり、GridView の列が乗算されて GridView にデータが入力されます。2 番目の列は、グリッドビューで作成される行数を示すためにユーザーが入力する値です。明らかに、この同じページに 2 つの列を持つグリッドビューがあります。
私がやろうとしているのは、ユーザーがテキストボックスのいずれかに値を入力すると、GridView が自動的に列と行を入力することです。どうやってやるの?
私のクラス:
public class myClass : INotifyPropertyChanged { int valueA = 0; int myRows = 0; private ObservableCollection myCollection = new ObservableCollection();
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public int ValueA
{
get { return valueA; }
set
{
valueA = value;
NotifyPropertyChanged();
}
}
public int MyRows
{
get { return myRows; }
set
{
myRows = value;
NotifyPropertyChanged();
}
}
private myGridValues PopulateGridValues()
{
myCollection.Clear();
double tempColumn2Value = 0;
for (int i = 0; i <= myRows - 1; i++)
{
myGridValues tempGridValueClass = new myGridValues(i + 1, tempColumn2Value);
tempColumn2Value = tempColumn2Value * valueA;
myCollection.Add(tempGridValueClass);
}
NotifyPropertyChanged();
return myCollection;
}
public ObservableCollection<myGridValues> MyCollection
{
get { return myCollection; }
}
class myGridValues
{
int column1Value = 0;
double column2Value = 0;
public myGridValues(int a, double b)
{
column1Value = a;
column2Value = b;
}
}
}My XAML for GridView<GridView x:Name="MyGridView" ItemsSource="{Binding Path=MyCollection}">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Column1Value}" Foreground="White" Margin="0,0,25,0"/>
<TextBlock Text="{Binding Column2Value}" Foreground="White" Margin="0,0,25,0"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
私の理解不足を許してください。グリッドビューに行を入力します。(マウスをグリッドビューの上に置くとそこにあることがわかりますが、各行のテキストボックスの値は空白です) 何が間違っていますか?
アプリがあり、メイン ページに 3 つのコントロールがあるとします。2 つのテキスト ボックスと 1 つのグリッドビュー。これをページのコンストラクターに入れましょう。
public myClass mc= new myClass();
public MainWindow()
{
this.InitializeComponent();
this.DataContext = mc; MyGridView.ItemSource = mc.MyCollection
...The two text boxes are just numerical values. The first textbox is how the values will be populated, and the second textbox tellsthe grid how many rows to make.So lets say that in the first textbox the user types in "2"And the second they type in 5. Meaning, "Do this 5 times"The grid has two columns, the first column is just the counter and the second column is the value. Therefore the grid would be populated as such1 22 43 84 165 32The text of both textboxes is bound to the class, "mc", properties
ご覧のとおり、ユーザーがいずれかのテキスト ボックスに値を入力すると、それらはクラスにバインドされているため、GridView は自動的に更新されます。列はありません。しかし、デバッグ モードでは、クラス mc の ObservableCollection プロパティに目的の値が実際に設定されていることがわかります。GridView のテキスト ボックスを正しくバインドしていませんか?