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;
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'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;
}
I have a wpf user control which contains a Listbox which is binded to my View Model. Each Listbox item is consists of three textblock. I made the UI in a way that when user clicks on the Frist TextBlock(Title) function in my View Model will get called.
To uniquely Identify the ListBox item corresponding to which text block now got clicked. I added another property in my view model which is bindied to SelectionChanged Event in the Listbox. So whenever my TextBlock binded command get exectuted I will use my SelectionChanged Property to find which List Box Item and use its data context.
But the issue I am facing now is that when User clicks on the First textblock, selectionchanged event is not firing. When User clicks outside the First Textblock only Selection Changed event is getting fired. Which made my view model to process wrong list box items.
Following is the XAML snippet.
<ListBox ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding SelectedNotificationItem}" ItemsSource="{Binding MyArray}" BorderThickness="0" Margin="0, 0, 0, 0" ItemContainerStyle="{StaticResource HoverBackgroundStyle}" Name="NotificationListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Border BorderThickness="4,0,0,0" BorderBrush="{Binding ColorThing, Converter={StaticResource SeverityToColorConverter} }" Margin="0, 0, 0, 0">
<StackPanel Margin="8, 0" Orientation="Vertical">
<TextBlock Style="{StaticResource HoverUnderlineStyle}" FontWeight="Bold" TextTrimming="CharacterEllipsis" Name="Title" Text="{Binding Title}" TextWrapping="WrapWithOverflow" Margin="0,4,0,0" >
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ClickTitleCommand}"></MouseBinding>
</TextBlock.InputBindings>
</TextBlock>
<TextBlock FontWeight="Normal" Name="Desc" Text="{Binding Description}" TextWrapping="WrapWithOverflow">
</TextBlock>
<TextBlock FontWeight="Normal" Foreground="Gray" Name="Date" Text="{Binding CreationTime, StringFormat={}{0:ddd MM.dd.yyyy} }" Margin="0,4,0,4">
</TextBlock>
</StackPanel>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Could you try changing your XAML to SelectedItem="{Binding SelectedNotificationItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Change the ListBox into an ItemsControl and change the orientation to horizontal
I seem to have run into something I can't quite solve. I have a longlistselector of letters databound to an observable collection of chars. I'd like to be able to click on the first item of that longlistselector and have something happen, but I can't seem to figure out how to access its onClick event.
Here is the code for the longlistselector
<phone:LongListSelector x:Name="uScrambleLLS" Margin="-12,0,0,540" ItemsSource="{Binding Scramble}" SelectionChanged="uScrambleLLS_SelectionChanged" LayoutMode="Grid" GridCellSize="50,50">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Tap="MyTextBlock_Tap" />
void MyTextBlock_Tap(object sender, RoutedEventArgs e)
{
TextBlock tb = (TextBlock)sender;
// Do stuff with the item
}
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.