-1
J'ai un DataGrid comme indiqué ci-dessousModifier DataGrid en-têtes de colonne dans WPF
<DataGrid x:Name="dataGrid" Margin="0,5,10,5" AutoGenerateColumns="False" ItemsSource="{Binding Products, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="productName" Binding="{Binding Path Name}" Header="Name To Change">
</DataGrid.Columns>
Pour le code J'utilise le DataContextSpy
public class DataContextSpy : Freezable
{
public DataContextSpy()
{
// This binding allows the spy to inherit a DataContext.
BindingOperations.SetBinding(this, DataContextProperty, new Binding());
}
public object DataContext
{
get { return GetValue(DataContextProperty); }
set { SetValue(DataContextProperty, value); }
}
// Borrow the DataContext dependency property from FrameworkElement.
public static readonly DependencyProperty DataContextProperty = FrameworkElement
.DataContextProperty.AddOwner(typeof(DataContextSpy));
protected override Freezable CreateInstanceCore()
{
// We are required to override this abstract method.
throw new NotImplementedException();
}
}
J'ai la MVVM viewmodel avec la propriété que je veulent lier l'en-tête à aimer ceci:
public class ViewModel: ViewModelBase{
string header1;
Public string Header1{
get{return header1;}
set{
Set(ref header1, value);
}
...........
My question is why is it not binding to the property? Anny suggestions?
Je vais marquer cela comme réponse, mais comment puis-je mettre en œuvre les changements de propriété. J'aimerais que les colonnes changent à l'exécution? – KMarto
Cela devrait juste être une question de définir la propriété source Header1 à une nouvelle valeur et augmenter l'événement PropertyChanged. Mais s'il vous plaît poser une nouvelle question si vous avez un autre problème. – mm8