我正在尝试将数据上下文和标签绑定到列表视图项。我的列表视图包含部门名称。因此用户可以选择任何部门。 问题是当我尝试检查第16个列表视图项目时,我没有得到数据上下文(包含部门ID)和标签(包含部门名称)。它对于第16个元素以上是完美的。(它们是否有任何滚动问题?因为在第15个元素之后,我需要向下滚动以检查该项目)
<ListView Width="480" Name="LVDepartment" ItemsSource="{Binding AllDepartmetnList}"
DataFetchSize="1"
IncrementalLoadingTrigger="Edge"
IncrementalLoadingThreshold="5" Margin="9,57,-3,57" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid BorderThickness="0 0 0 1" BorderBrush="#E1E1E1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<CheckBox Width="15" DataContext="{Binding id}" Tag="{Binding name}" Style="{StaticResource CheckBoxStyle1}" HorizontalAlignment="Left" VerticalAlignment="Top" Name="CheckBoxItem" Checked="CheckBoxItem_Checked" Unchecked="CheckBoxItem_Unchecked" Loaded="CheckBoxItem_Loaded" Grid.Column="0" ></CheckBox>
<TextBlock Width="465" Padding="20 0 0 0" Foreground="#333333" FontFamily="{StaticResource inventoryRegularFont}" FontSize="14px" HorizontalTextAlignment="Left" AccessKey="{Binding id}" HorizontalAlignment="Left" VerticalAlignment="Center" Name="TextblockItem" Grid.Column="1" Text="{Binding name}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后面的代码:=
public async void GetDepartmentList()
{
try
{
Uri requestUriForDept = new Uri(APIServer + "Department/GetDepartments");
var formattedJsonDataForDept = new
{
};
string jsonDataForDept = "";
jsonDataForDept = Newtonsoft.Json.JsonConvert.SerializeObject(formattedJsonDataForDept);
var objClintForUsers1 = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage responseForDept = await objClintForUsers1.PostAsync(requestUriForDept,
new StringContent(jsonDataForDept, System.Text.Encoding.UTF8, "application/json"));
string responJsonDataForDept = await responseForDept.Content.ReadAsStringAsync();
dynamic respondedDataForDept = JsonConvert.DeserializeObject(responJsonDataForDept.ToString());
if ((string)respondedDataForDept["status"] == "Success")
{
// Dept list initialization
DepartmentList = new ObservableCollection<DepartmentData> { };
foreach (var Dept in respondedDataForDept["deptList"])
{
string DeptName = Dept.deptName;
int DeptId = Dept.deptid;
AllDepartmetnList.Add(new DepartmentData(DeptId, DeptName) { });
}
LVDepartment.ItemsSource = AllDepartmetnList;
}
}
catch(Exception ex)
{
ex.Message.ToString();
}
}
选中复选框后:=
private void CheckBoxItem_Checked(object sender, RoutedEventArgs e)
{
CheckBox ch = (CheckBox)sender;
int DeptId = Convert.ToInt32(ch.DataContext); // here i am getting the datacontext zero if item is more than 16
}
我没有在上面的列表视图中选择项目数据上下文的16个。选中复选框后。