Hi iam using a listbox to show a list of items. in the listbox i ahve an togglebutton on every item. When i click on the toggle button the state of the togglebutton is pressed. But when i am scrolling down in the listbox and scolls up again. The togglebutton state is not pressed. How can i prevent this please help.
Heres my 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>
<!-- Chec开发者_Go百科kbox-->
<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>
Heres the binding code
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 });
}
}
And heres the Click event
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();
}
Is the value actually getting updated on the object that the ToggleButton is being bound to? ListViews in WPF use something called Virtualisation to only draw the portions of the listbox that are visible. This means that when your ToggleButton goes out of scope it gets redrawn (and re-calculated) when it comes back in to scope.
I think the problem lies in either the click method that the ToggleButton has, or the binding. Can you post more details about it?
After setting the checked togglebuttons (in the Listboxitem
) in the listbox, I had a Listbox.ScrollIntoView(CountriesListBox.Items[0]);
that made me loose the selections in the `Listbox.
I found the answer to be:
Listbox.Items.Refresh();
before the ScrollIntoView
.
精彩评论