您好我使用列表框来显示项目列表。在列表框中,我在每个项目上都有一个togglebutton。当我单击切换按钮时,按下了togglebutton的状态。但是当我在列表框中向下滚动并再次抬头时。没有按下togglebutton状态。我该如何防止这种情况请帮忙。
继承我的itemtemplate
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,3,0,0">
<Border BorderBrush="Black" BorderThickness="1,1,1,1">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
<GradientStop Color="#FFECECEC" Offset="1"/>
<GradientStop Color="#FFE8E8E8"/>
<GradientStop Color="#FFBDBDBD" Offset="0.153"/>
<GradientStop Color="#FFE8E8E8" Offset="0.904"/>
</LinearGradientBrush>
</Border.Background>
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter Property="Border.Height" Value="100"/>
<Setter Property="Border.Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
<GradientStop Color="DarkGray" Offset="1"/>
<GradientStop Color="#FFE8E8E8"/>
<GradientStop Color="#FFBDBDBD" Offset="0.153"/>
<GradientStop Color="DarkGray" Offset="0.904"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="500"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="55"/>
</Grid.ColumnDefinitions>
<!--Pick number-->
<StackPanel Grid.Column="0" VerticalAlignment="Center" Orientation="Vertical">
<TextBlock Text="{Binding Path=FtgNamn}" FontWeight="Bold" FontSize="22pt" FontFamily="Calibri"/>
<TextBlock Text="{Binding Path=LevsAttBeskr}" FontSize="18pt" FontFamily="Calibri"/>
</StackPanel>
<!--Pick Quantity-->
<StackPanel Grid.Column="1" VerticalAlignment="Center">
<TextBlock Text="{Binding Path=Antal}" FontSize="44pt" FontFamily="Calibri"/>
</StackPanel>
<!-- Checkbox-->
<StackPanel Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<ToggleButton Name="Check" Width="40" Height="40" Click="Check_Click" Tag="{Binding Path=Plocklista}">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="InnerBorder" Background="White" BorderBrush="Black" BorderThickness="1"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="InnerBorder" Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/Images/button_ok.png"/>
</Setter.Value>
</Setter>
<Setter TargetName="InnerBorder" Property="BorderThickness" Value="0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
</StackPanel>
</Grid>
<Border BorderBrush="Darkgray" BorderThickness="0,0,1,0">
</Border>
<TextBlock Width="100" Text="{Binding Path=Quantity}" FontSize="44pt" FontFamily="Calibri"/>
<CheckBox Width="78"/>
</StackPanel>
</Border>
</StackPanel>
</DataTemplate>
继承人的绑定代码
delegate void FillPickHeaderDelegate(PickHeaderDoc[] pickHeaderDocs);
private void FillPickList(PickHeaderDoc[] pickHeaderDoc) {
if(lbAllPickAssignments.Dispatcher.CheckAccess()) {
lbAllPickAssignments.ItemsSource = pickHeaderDoc;
lbAllPickAssignments.UnselectAll();
} else {
lbAllPickAssignments.Dispatcher.Invoke(new FillPickHeaderDelegate(FillPickList), new object[] { pickHeaderDoc });
}
}
继承人点击事件
private void Check_Click(object sender, RoutedEventArgs e) {
ToggleButton btn = (ToggleButton) sender;
if(btn.IsChecked.Value == true) {
m_checkedList.Add(Convert.ToInt32(btn.Tag));
} else {
m_checkedList.Remove(Convert.ToInt32(btn.Tag));
}
txtblockPickCount.Text = m_checkedList.Count.ToString();
}
答案 0 :(得分:0)
值是否实际更新了ToggleButton绑定的对象? WPF中的ListView使用称为虚拟化的东西来仅绘制列表框中可见的部分。这意味着当你的ToggleButton超出范围时,它会在重新进入范围时重新绘制(并重新计算)。
我认为问题在于ToggleButton的click方法或绑定。你能发布更多关于它的细节吗?
答案 1 :(得分:0)
在列表框中设置选中的切换按钮后(在Listboxitem
中,我有一个Listbox.ScrollIntoView(CountriesListBox.Items[0]);
,使我无法在“列表框”中进行选择。
我发现答案是:
Listbox.Items.Refresh();
在ScrollIntoView
之前。