我一直在搜索這樣的問題并在我的代碼中進行了很多更改,但我無法解決它。
我創建了一個 DataGrid:
<DataGrid Grid.Row="1" ItemsSource="{Binding VarDt}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="ID" Binding="{Binding Id}"/>
</DataGrid.Columns>
</DataGrid>
我使用的類的串列在 Main 模型中。
private List<ObjectVariable> _varDt;
public List<ObjectVariable> VarDt
{
get { return _varDt; }
set
{
_varDt = value;
OnPropertyChanged("VarDt");
}
}
當我第一次添加資料時,它可以作業,我可以在我的 DataGrid 中看到這些值:
public MainViewModel()
{
VarDt = new List<ObjectVariable>()
{
new ObjectVariable { Id="9846849", Name="Val1" },
new ObjectVariable { Id="fregreg", Name="Val2" },
new ObjectVariable { Id="cd6s8s6", Name="Val3" }
}; [...]
但是當我在執行命令時添加新專案(當我雙擊樹專案時),DataGrid 會更新。(我添加它不起作用的新專案的代碼):
private ICommand _selectItemCommand;
public ICommand selectItemCommand
{
get
{
return _selectItemCommand ?? (_selectItemCommand = new RelayCommand(param => this.LoadContent(param)));
}
}
private void LoadContent(object selectedMenuItem)
{
// NEW ITEM THAT IS NOT ADDED WHY???
VarDt.Add(new ObjectVariable { Id = "cw61851cw", Name = "ValPost" });
}
我不知道我是否沒有正確引發 PropertyChanged 事件以及如何做到這一點。
有人可以告訴我如何將資料正確添加到我的 DataGrid。
謝謝!
編輯 1:仍然無法使用可觀察集合
宣言:
private ObservableCollection<ObjectVariable> _varDt;
public ObservableCollection<ObjectVariable> VarDt
{
get { return _varDt; }
set
{
_varDt = value;
OnPropertyChanged("VarDt");
}
}
公共主視圖模型():
VarDt = new ObservableCollection<ObjectVariable>()
{
new ObjectVariable { Id="9846849", Name="ValuesAtStart1" },
new ObjectVariable { Id="fregreg", Name="ValuesAtStart2" },
new ObjectVariable { Id="cd6s8s6", Name="ValuesAtStart3" }
};
命令:
private void LoadContent(object selectedMenuItem)
{
// NEW ITEM THAT IS NOT ADDED WHY???
VarDt.Add(new ObjectVariable { Id = "cw61851cw", Name = "ValueInsideCommand" });
}
結果(未添加來自命令的資料):
未添加命令中的值
編輯 2:發現問題但我沒有解決方案
The DataGrid where I have the problem is inside another View that I manage with another class and a ContentControl:
Actually I have:
- MainViewModel and the MainView.
- VariablesViewModel and VariablesView that i put inside the ContentControl. The VariablesViewModel is empty, I only use it to show the VariablesView.
When I execute a command, I show the VariablesView in the ContentControl:
private void LoadContent(object selectedMenuItem)
{
TreeItemModel ItemSelected = (TreeItemModel)selectedMenuItem;
if (ItemSelected.Name == "Object variables")
{
// Current view is VariablesView
CurrentView = VariableVM;
// NEW ITEM THAT IS NOT ADDED WHY???
VarDt.Add(new ObjectVariable { Id = "cw61851cw", Name = "ValueInsideCommand" });
}
else CurrentView = "";
}
I realized that the DataGrid inside the VariablesView that I show in the ContentControl doesn't update the values, but if I put the same DataGrid in the Main View, the values are updated.
Maybe the problem is that "VarDt" is in the MainViewModel, not in VariablesViewModel, but I can show some data, (only the new values are not showed)
Test code:
[...]
<ContentControl Grid.Column="1"
Margin="10"
Content="{Binding CurrentView}"/>
<DataGrid Grid.Row="3" Grid.Column="2" ItemsSource="{Binding VarDt}" AutoGenerateColumns="True" />
[...]
Result of test Code: DataBinding result using ContentControl os DataGrid in MainView
編輯 3:完整背景關系
變數視圖:
<UserControl x:Class="AOE.MVVM.View.VariablesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewmodel="clr-namespace:AOE.MVVM.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AOE.MVVM.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<!--Asignación del archivo .cs a esta view-->
<UserControl.DataContext>
<viewmodel:MainViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions >
<RowDefinition Height="30*" MaxHeight="70"/>
<RowDefinition Height="100*"/>
<RowDefinition Height="10*" MaxHeight="30"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Image Source="/Media/Variables.png"
Height="50"
Width="50"
HorizontalAlignment="Left"/>
<TextBlock Text="Variables"
FontFamily="Verdana"
Foreground="#007BC0"/>
</StackPanel>
<DataGrid Grid.Row="1" ItemsSource="{Binding VarDt}" AutoGenerateColumns="True" >
</DataGrid>
</Grid>
VariablesViewModel:空類。
應用Xaml:
<Application x:Class="AOE.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AOE"
xmlns:viewModel="clr-namespace:AOE.MVVM.ViewModel"
xmlns:view="clr-namespace:AOE.MVVM.View"
StartupUri="MainWindow.xaml">
<Application.Resources>
<DataTemplate DataType="{x:Type viewModel:VariablesViewModel}">
<view:VariablesView/>
</DataTemplate>
</Application.Resources>
主視圖模型:
namespace AOE.MVVM.ViewModel
{
class MainViewModel : ObservableObject
{
public ObservableCollection<ObjectVariable> VarDt { get; set; }
//public TreeItemModel ItemSelected { get; set; }
private ICommand _selectItemCommand;
public ICommand selectItemCommand
{
get
{
return _selectItemCommand ?? (_selectItemCommand = new RelayCommand(param => this.LoadContent(param)));
}
}
private void LoadContent(object selectedMenuItem)
{
// Current view is VariablesView
CurrentView = VariableVM;
// NEW ITEM THAT IS NOT ADDED WHY???
VarDt.Add(new ObjectVariable { Id = "cw61851cw", Name = "ValueInsideCommand" });
}
public VariablesViewModel VariableVM { get; set; }
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged("CurrentView");
}
}
public MainViewModel()
{
MainTree.Add(MainObject);
VarDt = new ObservableCollection<ObjectVariable>()
{
new ObjectVariable { Id="9846849", Name="ValuesAtStart1" },
new ObjectVariable { Id="fregreg", Name="ValuesAtStart2" },
new ObjectVariable { Id="cd6s8s6", Name="ValuesAtStart3" }
};
VariableVM = new VariablesViewModel();
}
}
}
uj5u.com熱心網友回復:
VariablesView在 XAML 中明確定義MainViewModel為它DataContext。這就是為什么DataGrid顯示初始值:來自MainViewModel建構式的值。由于這是一個新實體,更改一個實體的值不會更改第二個實體的值。
解決此問題的兩個簡單解決方案。
一種解決方案可以是系結DataContext的VariablesView原來的MainViewModel實體。
您可以定義 aRelativeSource來查找父項ContentControl并將其DataContext用作DataContext系結的源:
變數視圖.xaml
<UserControl DataContext="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=DataContext}">
...
</UserControl>
第二種也是推薦的解決方案是使用自然的DataContext,即從DataTemplate.
結果是VariablesViewModel不能為空。如果MainViewModel和VariablesViewModel共享資料之類的VarDt屬性,則不要復制這些屬性。而結合依賴于該共享屬性明確地給對照DataContext所述的ContentControl(MainViewModel)。其他相關的非共享資料VariablesView必須移動到VariablesViewModel.
以下示例假定它VariablesView不是重用控制元件。不是,如果可重用性確實重要,則所有內部系結都必須參考繼承的DataContext. 這意味著Binding.RelativeSource必須避免在 UserControl 之外查找源。
變數視圖.xaml
<!-- DataContext is inherited from the DataTemplate and is the VariablesViewModel -->
<UserControl>
<!-- Bind explicitly to the shared data located in MainViewModel -->
<DataGrid ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=DataContext}" />
</UserControl>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/399871.html
上一篇:Flask登錄與Apache快取
