大家好,我遇到了数据网格内部复选框的问题。
首先让我发布我所获得的代码,然后我将告诉导致问题的原因
这是我在子窗口中的数据网格的代码
<Controls:DataGrid x:Name="dgAthlete" Height="Auto" Width="Auto"
IsReadOnly="True" AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Data, ElementName=dds}"
>
<Controls:DataGrid.Columns>
<Controls:DataGridTemplateColumn Header="CheckBoxColumn">
<Controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="cbAddAthlete" IsChecked="{Binding IsAdded}" Tag="{Binding}"
IsEnabled="True" Checked="cbAddAthlete_Checked" Unchecked="cbAddAthlete_Unchecked" />
</DataTemplate>
</Controls:DataGridTemplateColumn.CellTemplate>
<Controls:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsAdded,Mode=TwoWay}"/>
</DataTemplate>
</Controls:DataGridTemplateColumn.CellEditingTemplate>
</Controls:DataGridTemplateColumn>
<Controls:DataGridTemplateColumn>
<Controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding ImageFileName}"
Width="25" Height="25"
HorizontalAlignment="Stretch" />
</DataTemplate>
</Controls:DataGridTemplateColumn.CellTemplate>
</Controls:DataGridTemplateColumn>
<Controls:DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" />
<Controls:DataGridTextColumn Header="MiddleName" Binding="{Binding MiddleName}"/>
<Controls:DataGridTextColumn Header="LastName" Binding="{Binding LastName}"/>
<Controls:DataGridTextColumn Header="Email" Binding="{Binding Email}" />
<Controls:DataGridTextColumn Header="DOB" Binding="{Binding BirthDate}"/>
<Controls:DataGridTextColumn Header="Phone" Binding="{Binding PhoneNumber}"/>
<Controls:DataGridTextColumn Header="Website" Binding="{Binding WebSite}"/>
<Controls:DataGridTextColumn Header="Team" Binding="{Binding TeamName}"/>
<Controls:DataGridTextColumn Header="Club" Binding="{Binding ClubName}"/>
<Controls:DataGridTextColumn Header="County" Binding="{Binding CountyName}"/>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
<Controls:DataPager x:Name="dpAthlete" PageSize="4"
HorizontalAlignment="Stretch" Source="{Binding Data, ElementName=dds}"
Width="Auto"/>
</StackPanel>
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
这背后的代码是
public partial class AddAthletesToCompetition : ChildWindow
{
public ObservableCollection<Athlete> Athletes { get; set; }
public int CompetitionId { get; set; }
private PagedCollectionView pvcAthlete;
public AddAthletesToCompetition(int competitionId)
{
InitializeComponent();
CompetitionId = competitionId;
LoadAthlete();
Athletes = new ObservableCollection<Athlete>();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void LoadAthlete()
{
var context = new PublicServiceClient();
context.GetAthletesNotInCompetitionCompleted += context_GetAthletesNotInCompetitionCompleted;
context.GetAthletesNotInCompetitionAsync(CompetitionId);
}
void context_GetAthletesNotInCompetitionCompleted(object sender, GetAthletesNotInCompetitionCompletedEventArgs e)
{
if (e.Result != null)
{
pvcAthlete = new PagedCollectionView(e.Result.ToList());
dgAthlete.ItemsSource = pvcAthlete;
dpAthlete.Source = pvcAthlete;
}
}
//Checkbox Checked Event Hanlder
private void cbAddAthlete_Checked(object sender, RoutedEventArgs e)
{
var athlete = ((CheckBox)sender).Tag as AthleteBO;
if (athlete != null)
{
var ath = new Athlete();
ath.AthleteId = athlete.AthleteId;
Athletes.Add(ath);
}
}
//CheckBox unchecked Event Handler
private void cbAddAthlete_Unchecked(object sender, RoutedEventArgs e)
{
var athlete = ((CheckBox)sender).Tag as AthleteBO;
if (athlete != null)
{
var item = Athletes.First(i => i.AthleteId == athlete.AthleteId);
Athletes.Remove(item);
}
}
}
正如你所看到我正在使用分页一切正常,即当我们在第一页时检查和取消选中复选框工作的事件,但是假设你在第一页上检查了网格中的第一个和第二个项目,现在,一旦我转到下一页,我的网格正在执行它保留上一个视图并默认检查第二页上的第一个和第二个项目,这是不期望的行为
我在这个问题上经历了各种帖子,发现问题是视图是由datagrid注入的,但不知怎的,我找不到任何解决方案。
其他人正在谈论的是刷新可观察的集合并再次将其绑定到我的网格。
所以我有点困惑,在哪里绑定它以及何时更新可观察的集合。
我已经发布了所有代码,无论何时你回复,请提及我应该在哪里进行更改才能使用。
对于我正在使用WCF和EF的注释,所以我在这里的运动员列表将把这个列表发送到使用EF的WCF服务,它将被插入到数据库中。
我知道这不是Silverlight中的一个错误,但网格虚拟化的这一新增功能正在给我带来麻烦,所以应该可以解决这个问题。
谢谢
答案 0 :(得分:0)
将IsSelected bool属性添加到您的实体并将复选框控件绑定到它。此代码示例看起来像您使用的是.NET RIA服务和DomainDataSource(ItemsSource =“{Binding Data,ElementName = dds}”)控件,而不是WCF?我已将此解决方案与WCF一起使用,但未在分页设置中使用。试一试,让我们知道结果如何,
答案 1 :(得分:0)
我绑定了我的组合框但仍然遇到了问题,直到我开始使用双向绑定,现在似乎已经消失了