まず第一に、申し訳ありませんが、私は VB.Net にあまり詳しくないので、私のコードは C# で書かれています。私はあなたが実際に何を必要としているのか理解していないので、以下のあまりエレガントではない解決策しか提供できません.
ComboBox
データベースからアイテムにバインドするには、 a を使用する必要がありStaticResource
ます. それを機能させるには、次のようなクラスが必要です。
public class artiklsList : List<artikl>
{
public artiklsList()
{
this.Add(new artikl(1, "first")); //this is dummy items, you need to do a database stuff here
this.Add(new artikl(2, "second"));
this.Add(new artikl(3, "third"));
}
}
そして、次のような xaml:
<Window.Resources>
<my:artiklsList x:Key="source"></my:artiklsList>
</Window.Resources>
ComboBox
また、選択が変更されたときにセルをテキストで更新する必要があります。Binding
コントロールを使用してこれを行う最も簡単な方法は、 では機能しないため、単純な作業でElementName
はありませんDataGrid
。とにかく私がしたことはちょっとハックです...
したがって、xaml はそれほど複雑ではありません。
<DataGrid Name="dgrStavke" AutoGenerateColumns="False" Height="160" Width="600" HorizontalAlignment="Left" Margin="5" Grid.Row="7" Grid.ColumnSpan="4" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Artikl ID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedIndex="{Binding selectedIndexID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="cmbArtikli" Width="120" DisplayMemberPath="artiklId" ItemsSource="{StaticResource source}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Naziv artikla" Binding="{Binding nazivArtikla}"/>
</DataGrid.Columns>
</DataGrid>
テキストセルを更新するトリックを行うバインディングSelectedIndex
と恐ろしいコードビハインド。バインディングが適切に機能するためにarticl
は、クラスはINotifyPropertyChanged
インターフェイスを実装する必要があります。
public class artikl: INotifyPropertyChanged
{
public artikl(int artid, string nazivart)
{
artiklId = artid;
nazivArtikla = nazivart;
}
public int artiklId{get;set;}
private string _nazv;
public string nazivArtikla
{
get { return _nazv; }
set { _nazv = value; NotifyPropertyChanged("nazivArtikla"); }
}
//Here I think you may have questions
private int _index;
public int selectedIndexID
{
get
{
//To get a SelectedIndex for ComboBox in current row we look in
//listArtikli defined in a MainWindow for a articli item with a current
//item's Id and take the index of this item
artikl art = MainWindow.listArtikli.Find(el => el.artiklId == this.artiklId);
return MainWindow.listArtikli.IndexOf(art);
}
set
{
//This property is binded with SelectedIndex property of ComboBox.
//When selected index changed, we look in listArtikli and take
//here properties of item with this index.
//This will change values of item binded to the current grid row
_index = value;
this.nazivArtikla = MainWindow.listArtikli[value].nazivArtikla;
this.artiklId = MainWindow.listArtikli[value].artiklId;
NotifyPropertyChanged("selectedIndexID");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
そして、ウィンドウの分離コード:
public partial class MainWindow : Window
{
public static artiklsList listArtikli = new artiklsList();
public static artiklsList gridsource = new artiklsList();
public MainWindow()
{
InitializeComponent();
dgrStavke.ItemsSource = gridsource;
}
}
あなたの価値観を埋めることはgridsource
別として必要です。また、その値のみを使用すると、このすべてのコードが破損するためです。そのため、取得された順に並べ替えられたアイテムが含まれます。で示したように、ペアが含まれています。少しでもお役に立てれば幸いです。listArtikli
DataGrid
selectedIndexID
listArtikli
listArtikli
articli
gridsource
artiklId
nazivArtikla
DataGrid