我正在研究一个数据网格,一切正常,直到我观察到我是否删除了第一行,我遇到了很多问题。
目标
DataGrid绑定到可观察集合
用户可以添加新行并删除它们,但我至少需要一个默认行。
所以XAML代码是:
<toolkit:DataGrid x:Name="MainGrid"
ItemsSource="{Binding StrategieList}"
SelectedItem="{Binding SelectedStrategie, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ignoreNewItems}, Mode=TwoWay }"
AutoGenerateColumns="False"
DockPanel.Dock="Top"
MouseMove="OnMainGridMouseMove"
DragEnter="MainGrid_DragEnter_1"
DragLeave="OnMainGridCheckDropTarget"
DragOver="OnMainGridCheckDropTarget"
Drop="OnMainGridDrop" DataContextChanged="OnMainGridDataContextChanged"
CanUserAddRows="False">
由于某些个人原因,我不会在这里向你展示Observable集合中的一些代码behinf。
在模型视图中,您可以看到可观察集合的管理:
模型视图构造函数:
public ControlorStrategies(){
_pStrategie = new ObservableCollection<StrategieDescription>();
_pStrategie.CollectionChanged += AvailableStrategyChanged;
_selectedstrategie.PropertyChanged += ListIdChanged;
}
现在代表是:
private void AvailableStrategyChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
this._pStrategie.Last().PropertyChanged += ListIdChanged; //subscribe the latest element add to the events
//Warning create the strategy modifying the current strategies
this.list_of_strategies.AddNewStrategie();
for (int i = 0; i < _pStrategie.Count; i++)
StrategieList[i].HasNewButton = false;
StrategieList.Last().HasNewButton = true;
StrategieList.Last().StrategyNumber = StrategieList.Count-1;
StrategieList.Last().PropertyChanged += ListIdChanged;
}
//we delete the strategy
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
//in case there is no more strategies the objects should be liberated
if (StrategieList.Count == 0)
{
_pStrategie.CollectionChanged -= AvailableStrategyChanged;
list_of_strategies.ListOfStrategies.RemoveAt(e.OldStartingIndex);
_pStrategie.Add(new StrategieDescription());
_pStrategie.CollectionChanged += AvailableStrategyChanged;
_pStrategie.Last().PropertyChanged += ListIdChanged;
}
//Affect the normal color to the objects
else
{
//Delete the users data affected to the objects
DeleteUserDatas.SuppressUserDatasWithId(SelectedStrategie.ListId);
list_of_strategies.ListOfStrategies.RemoveAt(e.OldStartingIndex);
_pStrategie.Last().HasNewButton = true;
}
}
if (StrategieList.Count > 0)
first_id = StrategieList.First().ListId;
}
现在我可以解释一下这个问题: 数据网格在列中划分并且工作得非常好,除非我删除行,第一行的行为并不完全按照我的要求工作 - &gt;更准确地说,我不能删除第一行上的按钮。 Loo在XAML代码中理解
<toolkit:DataGridTemplateColumn Header="">
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="AddStrat" Content="Add new strategie" Visibility="{Binding HasNewButton, Converter={StaticResource BoolToVis}}" Click="AddStrat_Click_1" />
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
有没有人知道为什么在擦除每一行时,只有第一行表示按钮的值也等于false?