I'm trying to build a ListBox which allows the user to unselect the item.
This is my XAML code:
<ListBox Name="MyListBox"
ItemsSource="{Binding MoeglicheHauptspeisen}"
SelectionMode="Single">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="200"
Height="200"
Background="{StaticResource Braun}"
MouseDown="Speise_Gedrueckt">
<TextBlock Margin="0 50 0 0"
HorizontalAlignment="Center"
Text="{Binding SpeiseTyp}" />
<TextBlock HorizontalAlignment="Center" Text="{Binding Beschreibung}" />
<TextBlock HorizontalAlignment="Center" Text="{Binding Preis, StringFormat={}{0:C}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And my code-behind:
private void Speise_Gedrueckt(object sender, MouseButtonEventArgs e)
{
MyListBox.UnselectAll();
}
I can unselect the item by clicking on it again, it does set SelectedIndex to -1 etc., but it doesn't clear the border that the selected item has in the ListBox itself.
I did google a lot but nothing I found did change that fact. I tried to set the border/background of the item which is selected via a style.setter, but it also didn't help.
Pay attention that Listbox item has 3 visual states - Selected, Not selected, and Focused. Even if you deselect Listbox item through code-behind, keyboard focus will still be there (assuming that you selected it by clicking on it). That's why some kind of visual indicator will be shown.
Not sure when do you want to clear the selected item so I added a button with the EventHandler:
<Button Width="100" Height="40" Click="Speise_Gedrueckt"></Button>
Now set the SelectedItem to null when the button is clicked:
private void Speise_Gedrueckt(object sender, RoutedEventArgs routedEventArgs)
{
MyListBox.SelectedItem = null;
}
Related
I have a comboBox that is styled to have rows of text blocks and check boxes to allow user to check a list of different things. What I am attempting to do is disable selection of a row in the the combobox.
My View Code:
<ComboBox ItemsSource="{Binding HeaderList}"
IsSelected="False"
HorizontalAlignment="Left"
Height="10"
x:Name="ComboBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"/>
<TextBlock Text="{Binding HeaderName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here is a picture of my Drop Down:
Here is a picture of what I don't want:
I want to disable the ability for the user to select a specific row, but keep the ability for them to select various check boxes in the drop down. Is there a way to style this?
I had the same issue, i solved it by using a ToggleButton in combination with a Popup (instead of using a ComboBox)
Note: Not tested
<ToggleButton x:Name="filterButton" />
<Popup x:Name="popup"
AllowsTransparency="True"
StaysOpen="False"
PlacementTarget="{Binding ElementName=filterButton}"
IsOpen="{Binding ElementName=filterButton,Path=IsChecked,Mode=TwoWay}">
<ItemsControl ItemsSource="{Binding HeaderList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"/>
<TextBlock Text="{Binding HeaderName}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Popup>
You can try to set the selectedindex of your combobox to -1 in the selectionchanged event
private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
myComboBox.SelectedIndex = -1;
}
I have 10 checkboxes in a listbox in windows phone.
My xaml file for checkbox into listbox is given below:
<ListBox x:Name="notificationSettingsListBox" Grid.Row="1" Margin="20,20,20,20" Background="#e79e38" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="#055cc3" Width="500" Height="200">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding channel_name}" Foreground="White" FontSize="31" TextAlignment="Left" TextWrapping="Wrap" />
<CheckBox Content="Enable Notification" Checked="pushNotiOnCheckBox_Checked" Unchecked="pushNotiOnCheckBox_Unchecked"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can I access all checkboxes one by one like checkbox1.checked=true or checkbox2.checked=false and so on?
I see that you subscribe to both Checked and Unchecked events, one thing you could do is add this in the handlers
var box = (sender as CheckBox);
var value = box.IsChecked;
I have a ListPicker with a SelectionChanged Event. I want the SelectionChange event to only fire when I change the selection, obviously, but in my case, it is also firing whenever I load the page.
Code:
<toolkit:ListPicker x:Name="sightingTypesPicker" ItemsSource="{Binding sightingTypes, ElementName=this}" SelectionChanged="sightingTypesPicker_SelectionChanged">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
<TextBlock Visibility="Collapsed" Text="{Binding TypeId}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
C#:
private void sightingTypesPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SightingType selecteditem = e.AddedItems[0] as SightingType;
Sighting.Instance.TypeId = selecteditem.TypeID;
}
Basically by defaut a list picker or a list box or a combo box or any drop down has a particular selected item and that is the zeroth item of the listbox./.. etc. So when the page is loading and data is being populated to the list boxes or else it fires the selection change from -1 ie no dat state to 0 selection changed to default value :)
You can add the handler on the Loaded event handler in your code so you ensure it's only called after that.
Update:
For example add this code in Loaded event of your page instead of XAML:
sightingTypesPicker.SelectionChanged += sightingTypesPicker_SelectionChanged;
I want to drag data from a ListView and drop it in a TreeView(the draging works fine). I use DataBinding and ItemTemplate to fill the TreeView.
<TreeView ItemsSource="{Binding Groups}" Name="tvGroups" AllowDrop="True"
Drop="tvDrop" DragOver="tvDragOver">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Participants}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<Button Tag="{Binding .}" Click="Button_Click_2">
<Image Source="Resources/cross.png" />
</Button>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Alias}" />
<Button Tag="{Binding .}" Name="btnDeleteParticipants" Click="btnParticipants_Click" >
<Image Source="Resources/cross.png" />
</Button>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
private void tvDrop(object sender, DragEventArgs e)
{
if (e.Effects == DragDropEffects.Copy &&
e.Data.GetDataPresent(typeof(Participant)))
{
Participant data = e.Data.GetData(typeof(Participant)) as Participant;
}
}
A Participant is dragged from the ListView to the TreeView. Now I need to find the Group. Any ideas where to get the right Group from the TreeView?
I would simply set the Drop="tvDrop" and DragOver="tvDragOver" on the StackPanel in the HierarchicalDataTemplate's ItemTemplate.
This way
1) You don't have any risk of getting an event when something is dropped out of a group
2) You can safely cast the Sender to a FrameworkElement and get the DataContext and cast it to your class.
You can also set a different handler on the treeview itself if you need to support dragging out of the groups.
I have a ListBox that has a custom DataTemplate assigned to it, so that it can correctly display it's content - a custom "Accessory" (which consists of three string properties) object per row. Additionally, there is a button on each row. That button should trigger an event that adds the selected accessory to a MemoryList. Here is the DataTemplate:
<DataTemplate x:Key="AccessoryListBoxTemplate">
<StackPanel>
<!--Truncated-->
<TextBlock FontFamily="Avenir Next LT Pro" VerticalAlignment="Center" FontSize="14" Text="{Binding Path=AgilityHeader}" Margin="3,0,0,0" Grid.Column="0" />
<TextBlock FontFamily="Avenir Next LT Pro" VerticalAlignment="Center" FontSize="14" Text="{Binding Path=ItemNumber}" Grid.Column="1" />
<TextBlock FontFamily="Avenir Next LT Pro" HorizontalAlignment="Right" VerticalAlignment="Center" Text="{Binding Path=Price}" FontSize="14" Grid.Column="2" />
<Button x:Name="ButtonAccessoryAddToMemoryList" VerticalAlignment="Center" Click="buttonAccessoryAddToMemoryList_Click" HorizontalAlignment="Right" FontSize="14" Width="80" Grid.Column="3" Margin="0,5,0,5">Minneslista</Button>
</Grid>
</StackPanel>
</StackPanel>
</DataTemplate>
And here is the ListBox:
<ListBox Grid.ColumnSpan="3" Grid.Row="1" BorderThickness="0" x:Name="ListBoxAccessories" ItemTemplate="{StaticResource AccessoryListBoxTemplate}" HorizontalContentAlignment="Stretch" ItemsSource="{Binding}" SelectedIndex="-1" IsEnabled="True" />
The problem I'm having is this - I cannot reliably identify on which row ButtonAccessoryAddToMemoryList is clicked, since the row that the button is on is not set as the SelectedItem for the ListBox if the user doesn't first select the row and then push the button - and honestly, who does that? :)
How should I go about identifying which button was pressed? Any help would be greatly appreciated. Thanks!
[EDIT] Thanks to Chadwick for that answer. Works perfectly. [/EDIT]
If what you're really after is to know which Accessory object was clicked on you can set the Tag property of the button:
<Button x:Name="ButtonAccessoryAddToMemoryList" Tag="{Binding}" Click="buttonAccessoryAddToMemoryList_Click" ... >Minneslista</Button>
and then cast out the object in the click handler:
private void ButtonAccessoryAddToMemoryList(object sender, RoutedEventArgs e)
{
Button b = e.Source as Button;
Accessory a = b.Tag as Accessory;
Try out M-V-VM pattern and a command binding. If the datatemplate were bound to its own object and the command were activated, then you would already know the record being clicked.
In the XAML, you could give the button a _Loaded event handler. Then, in the code behind, set the _Click event. Perhaps make an array of delegates so that the clicks will call different handlers.