I have a grid view with the following Group Style Header template:
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
Command="{Binding GroupCommand}"
Style="{StaticResource TextPrimaryButtonStyle}"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
The problem is that when I click the button, the GroupCommand does not execute.
What can be the problem here ?
you should try this way:
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
SampleDataItem item = new SampleDataItem("test", "test!", "subtitle", "", "", "", null);
SampleDataSource.AddItemToFirstGroup(item);
}
private void Button_Tapped_2(object sender, TappedRoutedEventArgs e)
{
SampleDataGroup group = new SampleDataGroup("test", "testGroup", "subtitle", "", "");
group.Items.Add(new SampleDataItem("test", "test!", "subtitle", "", "", "", null));
SampleDataSource.AddGroup(group);
}
I got it from here
WinRT (Win 8) Store App XAML Bindings RelativeSourceMode FindAncestor missing?
I wanted to bind my button to an ICommand in my ViewModel, so I did it like this:
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
Command="{Binding ElementName=myParentGridView, Path=DataContext.GroupCommand}"
Style="{StaticResource TextPrimaryButtonStyle}"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
Related
I have a Form in which there are many labels and textfield. So I'm navigating within the textfield or entry by clicking on tab button in my keyboard. The normal entry is getting focused on single click whereas the AutoSuggestBoxView which is a custom entry needs two clicks on tab to get focused. I don't know why I'm facing this issue. I have applied tabindex as 0 and -1 but still it is not working.This is my xaml code
<CtrlUC:UCCurrentLocation Grid.Row="0" HeightRequest="32" Grid.Column="0" Latitude="{Binding FieldsData.fromlatitude.Value,Mode=TwoWay}" Longitude="{Binding FieldsData.fromlongitude.Value,Mode=TwoWay}" Grid.RowSpan="2" Grid.ColumnSpan="2" HorizontalOptions="Fill" VerticalOptions="Fill" WidthRequest="400" Title="Location" LatitudeTitle="Latitude" LongitudeTitle="Longitude" ButtonText="Location" IsEditable="False" IsVisible="{Binding Path=FieldsData.FromLocationControl.IsVisible, Mode=TwoWay}" IsEnabled="{Binding Path=FieldsData.IsViolationCreated.IsEnabled, Mode=TwoWay}" />
<local:AutoSuggestBoxView x:Name="FromState" Grid.Row="1" Grid.Column="2" TabIndex="-1" Grid.RowSpan="1" Grid.ColumnSpan="1" DisplayMemberPath="{Binding FieldsData.PickerFromState.Value.DisplayField}" SelectedValuePath="{Binding FieldsData.PickerFromState.Value.ValueField}" SearchMemberPath="{Binding FieldsData.PickerFromState.Value.DisplayField}" VerticalOptions="StartAndExpand" ItemsSource="{Binding Path=FieldsData.PickerFromState.Value.DataSource}" SelectedItem="{Binding Path=FieldsData.PickerFromStateSelectedItem.Value, Mode=TwoWay}" SelectedValue="{Binding Path=FieldsData.fromstatePropertyText.Value, Mode=TwoWay}" Text="{Binding Path=FieldsData.fromstatename.Value, Mode=TwoWay}" Placeholder="Enter State" IsEnabled="{Binding Path=FieldsData.IsViolationCreated.IsEnabled, Mode=TwoWay}" IsVisible="{Binding Path=FieldsData.PickerFromState.IsVisible, Mode=TwoWay}" />
<local:CCEntry Grid.Row="1" Grid.Column="3" Style="{StaticResource FormEntryStyle}" Margin="0,0,10,3" Text="{Binding Path=FieldsData.fromstatecode.Value, Mode=TwoWay}" Grid.RowSpan="1" Grid.ColumnSpan="1" HorizontalOptions="Fill" VerticalOptions="Fill" IsEnabled="{Binding Path=FieldsData.fromstatecode.IsEnabled, Mode=TwoWay}" IsVisible="{Binding Path=FieldsData.LblFromStateCodeValue.IsVisible, Mode=TwoWay}" />
<local:AutoSuggestBoxView x:Name="FromCity" Grid.Row="1" Grid.Column="4" TabIndex="" Grid.RowSpan="1" Grid.ColumnSpan="1" DisplayMemberPath="{Binding FieldsData.PickerFromCity.Value.DisplayField}" SelectedValuePath="{Binding FieldsData.PickerFromCity.Value.ValueField}" SearchMemberPath="{Binding FieldsData.PickerFromCity.Value.DisplayField}" VerticalOptions="StartAndExpand" ItemsSource="{Binding Path=FieldsData.PickerFromCity.Value.DataSource}" SelectedItem="{Binding Path=FieldsData.PickerFromCitySelectedItem.Value, Mode=TwoWay}" SelectedValue="{Binding Path=FieldsData.fromcityPropertyText.Value, Mode=TwoWay}" Text="{Binding Path=FieldsData.fromcityname.Value, Mode=TwoWay}" Placeholder="Enter City" IsEnabled="{Binding Path=FieldsData.IsViolationCreated.IsEnabled, Mode=TwoWay}" IsVisible="{Binding Path=FieldsData.PickerFromCity.IsVisible, Mode=TwoWay}" />
<local:CCEntry Grid.Row="1" Grid.Column="5" Style="{StaticResource FormEntryStyle}" Margin="0,0,10,3" Text="{Binding Path=FieldsData.fromcitycode.Value, Mode=TwoWay}" Grid.RowSpan="1" Grid.ColumnSpan="1" HorizontalOptions="Fill" VerticalOptions="Fill" IsEnabled="{Binding Path=FieldsData.fromstatecode.IsEnabled, Mode=TwoWay}" IsVisible="{Binding Path=FieldsData.LblFromCityCodeValue.IsVisible, Mode=TwoWay}" />
<local:AutoSuggestBoxView x:Name="FromCounty" Grid.Row="1" Grid.Column="6" TabIndex="0" Grid.RowSpan="1" Grid.ColumnSpan="1" DisplayMemberPath="{Binding FieldsData.PickerFromcounty.Value.DisplayField}" SelectedValuePath="{Binding FieldsData.PickerFromcounty.Value.ValueField}" SearchMemberPath="{Binding FieldsData.PickerFromcounty.Value.DisplayField}" VerticalOptions="StartAndExpand" ItemsSource="{Binding Path=FieldsData.PickerFromcounty.Value.DataSource}" SelectedItem="{Binding Path=FieldsData.PickerFromCountySelectedItem.Value, Mode=TwoWay}" SelectedValue="{Binding Path=FieldsData.fromcountyPropertyText.Value, Mode=TwoWay}" Text="{Binding Path=FieldsData.fromcountyname.Value, Mode=TwoWay}" Placeholder="Enter County" IsEnabled="{Binding Path=FieldsData.IsViolationCreated.IsEnabled, Mode=TwoWay}" IsVisible="{Binding Path=FieldsData.PickerFromcounty.IsVisible, Mode=TwoWay}" />
<local:CCEntry Grid.Row="1" Grid.Column="7" Style="{StaticResource FormEntryStyle}" Margin="0,0,10,3" Text="{Binding Path=FieldsData.fromcountycode.Value, Mode=TwoWay}" Grid.RowSpan="1" Grid.ColumnSpan="1" HorizontalOptions="Fill" VerticalOptions="Fill" IsEnabled="{Binding Path=FieldsData.fromstatecode.IsEnabled, Mode=TwoWay}" IsVisible="{Binding Path=FieldsData.LblFromCountyCodeValue.IsVisible, Mode=TwoWay}" />
So as you can see in code the normal entry like CCEntry takes only one tab click. But the custom entry AutoSuggestBoxView takes two clicks on tab to get focused.I have no clue how to fix this. Any suggestions?
There is only one way to avoid multiple clicks in xamarin.forms is you have to use a boolean flag as shown below
public class MyPage : ContentPage
{
public isClicked = false;
public MyPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
isClicked = false;
}
private void ItemTapped(object sender, System.EventArgs e)
{
if(isClicked)
return;
isClicked = true;
//Do your work here
}
}
I am using this way to avoid multiple clicks.
How to show one listview to multiple xaml?
on mainpage.xaml, i just want to show binding "Kata". another xaml i want to show binding "Kata" and "Description".
this mainpage.xaml
<ListView x:Name="listView" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="520" VerticalAlignment="Top" Width="340" Margin="10,10,0,0" Grid.Row="2" Background="#33FFFFFF">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Kata}" Tapped="TextBlock_Tapped_1" FontSize="26.667" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
this another xaml.
<ListView x:Name="dictionary_list" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="620" VerticalAlignment="Top" Width="340" Margin="10,10,0,0" Background="{ThemeResource AppBarItemDisabledForegroundThemeBrush}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Kata}" FontSize="26.667" Foreground="White" TextAlignment="Center" />
<TextBlock Text="{Binding Description}" Foreground="White" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Use UserControl and one DependencyProperty whether to show or not description field
ListViewUserContrl.xaml
<UserControl
x:Class="Listview.ListViewUserContrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Listview"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="ListViewUserControl">
<Grid>
<ListView x:Name="dictionary_list" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="620" VerticalAlignment="Top" Width="340" Margin="10,10,0,0" Background="{ThemeResource AppBarItemDisabledForegroundThemeBrush}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Kata}" FontSize="26.667" Foreground="White" TextAlignment="Center" />
<TextBlock Text="{Binding Description}" Visibility="{Binding ElementName=ListViewUserControl,Path=DescriptionVisibility}" Foreground="White" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</UserControl>
ListViewUserContrl.xaml.cs
public sealed partial class ListViewUserContrl : UserControl
{
public static readonly DependencyProperty DescriptionVisibilityProperty =
DependencyProperty.Register("DescriptionVisibility", typeof(Visibility), typeof(ListViewUserContrl),
new PropertyMetadata(Visibility.Collapsed));
public Visibility DescriptionVisibility
{
get { return (Visibility)GetValue(DescriptionVisibilityProperty); }
set { SetValue(DescriptionVisibilityProperty, value); }
}
public ListViewUserContrl()
{
this.InitializeComponent();
}
}
Add this to your page like this
<local:ListViewUserContrl DescriptionVisibility="Visible"/>
I have this ListBox with custom elements and I want them to stretch to take all the available area. This is my code; currently the items only take the space that they need and I have some unused space on the left and the right side of the screen. Why?
<ListBox x:Name="listBox" Margin="0,6,0,0">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:ListTemplateSelector Content="{Binding}">
<local:ListTemplateSelector.bloccato>
<DataTemplate>
<StackPanel Grid.Row="1">
<Grid Background="Beige">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="/Assets/Images/locked.png" Width="40" Height="40" HorizontalAlignment="Left"/>
<TextBlock Grid.Column="1" Text="{Binding nomePacchetto}" FontFamily="./Assets/neo-normal.ttf#NEOTERIC" FontSize="48" VerticalAlignment="Bottom" Foreground="#FF373737"/>
</Grid>
<Line Stroke="DarkGray" X2="400" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,6,0,6"/>
</StackPanel>
</DataTemplate>
</local:ListTemplateSelector.bloccato>
<local:ListTemplateSelector.sbloccato>
<DataTemplate>
<StackPanel Grid.Row="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="left" Foreground="#FF5B5B5B"><Run FontSize="32" Text="1/"/> <Run FontSize="20" Text="40"/></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding nomePacchetto}" FontFamily="./Assets/neo-normal.ttf#NEOTERIC" FontSize="48" VerticalAlignment="Bottom" Foreground="#FF373737"/>
</Grid>
<Line Stroke="DarkGray" X2="400" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,6,0,6"/>
</StackPanel>
</DataTemplate>
</local:ListTemplateSelector.sbloccato>
</local:ListTemplateSelector>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
There are two ways to solve this problem:
(1)
The easiest way is giving fixed width to your root grid in datatemplate. Though you'll provide fixed width, it will be resolution responsive.
Check this example:
// XAML page
<ListBox x:Name="lbxTest" Margin="12">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Blue" Width="370" >
<TextBlock Text="{Binding}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If you write your code behind like this:
List<string> lstString = new List<string>() { "string 1", "string 2", "string 3" };
lbxTest.ItemsSource = lstString;
Then in every resolution (480x800, 720x1280, 768x1280, 1080x1920), the size obtained by ListBox will be same.
check the screenshots for refference.
screenshot in 480x800
screenshot in 768x1280
(2)
The other way to solve this problem is adding one parameter in the ItemSource we'll be assigning to the ListBox.
<ListBox x:Name="lbxTest" Margin="12">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Blue" Width="{Binding width}" >
<!--Width="370"-->
<TextBlock Text="{Binding text}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
// code behind
public class Model
{
public double width { get; set; }
public string text { get; set; }
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Model m1 = new Model()
{
text = "string 1",
width = lbxTest.ActualWidth
};
Model m2 = new Model()
{
text = "string 2",
width = lbxTest.ActualWidth
};
List<Model> lstModel = new List<Model>();
lstModel.Add(m1);
lstModel.Add(m2);
lbxTest.ItemsSource = lstModel;
}
Hope this will help..!!
There are two properties that should work but they do not (HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch")
To solve that this example will help you with the witdh:
<ListBox x:Name="ListBoxInstance" HorizontalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Green" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="{Binding ActualWidth, ElementName=ListBoxInstance, Mode=OneWay}" >
<Border Background="Red" >
<TextBlock Text="{Binding}"/>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<x:String>This is a test</x:String>
<x:String>This is the second</x:String>
<x:String>This is the thidr</x:String>
<x:String>s</x:String>
</ListBox.Items>
</ListBox>
As you see with
Width="{Binding ActualWidth, ElementName=ListBoxInstance}"
You have the content to the full width.
In some others cases (depending on the platform)
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
I want to select the list item when I click the button(Which is inside the list box for every row).
Now I have try like this:-
<ListBox Height="444"
ItemsSource="{Binding StudentDetails,Mode=TwoWay}"
HorizontalAlignment="Left" Margin="2,34,0,0"
Name="listBox1" VerticalAlignment="Top"
Width="476" BorderBrush="#00410D0D"
SelectedIndex="{Binding MemberPrivacy,Mode=TwoWay}"
SelectedItem="{Binding SelectedStudent, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" Padding="5" BorderThickness="1">
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Wheat" BorderThickness="1">
<Image Name="ListPersonImage" Source="{Binding PersonImage}" Height="100" Width="100" Stretch="Uniform" Margin="10,0,0,0"/>
</Border>
<TextBlock Text="{Binding FirstName}" Name="firstName" Width="200" Foreground="White" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22" />
<Image Height="50" Source="{Binding addImage}" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="50" />
<Button Margin="-100,0,0,0" Height="80" Width="80" DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}" >
<Button.Background>
<ImageBrush ImageSource="{Binding addImage, Converter={StaticResource pathToImageConverter}}" Stretch="Fill" />
</Button.Background>
</Button>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My View Model:-
public ListBoxEventsViewModel()
{
addPerson = new ReactiveAsyncCommand();
addPerson.Subscribe(x =>
{
MessageBox.Show("Test Button Selected..");
});
}
Here I can show the message box when I click the button. But I can not select the list item. Please let me know to solve this problem.
Thanks in advance.
View Model:-
List box selected Item:-
public ListBoxEventsModel _SelectedStudent;
public ListBoxEventsModel SelectedStudent
{
get { return _SelectedStudent; }
set
{
this.RaiseAndSetIfChanged(x => x.SelectedStudent, value);
MessageBox.Show("Selected index==>" + SelectedStudent.FirstName);
}
}
Here it showing the selected name when I click the list item. But this same thing I want to write it for the button(addImage)
Bind the SelectedItem on the ListBox to a "SelectedItem" property on the ViewModel.
eg SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
then on the view model just add a property of SelectedItem, then you can just set that from in your subscribe.
I'm currently playing with the Silverlight(Beta 2) Datagrid control. Before I wired up the SelectionChanged event, the grid would sort perfectly by clicking on the header. Now, when the grid is clicked, it will fire the SelectionChanged event when I click the header to sort. Is there any way around this?
In a semi-related topic, I'd like to have the SelectionChanged event fire when I click on an already selected item (so that I can have a pop-up occur to allow the user to edit the selected value). Right now, you have to click on a different value and then back to the value you wanted in order for it to pop up. Is there another way?
Included is my code.
The Page:
<UserControl x:Class="WebServicesApp.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
Width="1280" Height="1024" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" x:Name="OurStack" Orientation="Vertical" Margin="5,5,5,5">
<ContentControl VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel x:Name="SearchStackPanel" Orientation="Horizontal" Margin="5,5,5,5">
<TextBlock x:Name="SearchEmail" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="Email Address:" Margin="5,5,5,5" />
<TextBox x:Name="InputText" HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="150" Height="Auto" Margin="5,5,5,5"/>
<Button x:Name="SearchButton" Content="Search" Click="CallServiceButton_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Height="Auto" Background="#FFAFAFAF" Margin="5,5,5,5"/>
</StackPanel>
</ContentControl>
<Grid x:Name="DisplayRoot" Background="White" ShowGridLines="True"
HorizontalAlignment="Center" VerticalAlignment="Center" MaxHeight="300" MinHeight="100" MaxWidth="800" MinWidth="200"
ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
<data:DataGrid ItemsSource="{Binding ''}" CanUserReorderColumns="False" CanUserResizeColumns="False"
AutoGenerateColumns="False" AlternatingRowBackground="#FFAFAFAF" SelectionMode="Single"
HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,5,5,5" x:Name="IncidentGrid" SelectionChanged="IncidentGrid_SelectionChanged">
<data:DataGrid.Columns>
<data:DataGridTextColumn DisplayMemberBinding="{Binding Address}" Header="Email Address" IsReadOnly="True" /> <!--Width="150"-->
<data:DataGridTextColumn DisplayMemberBinding="{Binding whereClause}" Header="Where Clause" IsReadOnly="True" /> <!--Width="500"-->
<data:DataGridTextColumn DisplayMemberBinding="{Binding Enabled}" Header="Enabled" IsReadOnly="True" />
</data:DataGrid.Columns>
</data:DataGrid>
</Grid>
</StackPanel>
<Grid x:Name="EditPersonPopupGrid" Visibility="Collapsed">
<Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.765" Fill="#FF8A8A8A" />
<Border CornerRadius="30" Background="#FF2D1DCC" Width="700" Height="400" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1,1,1,1" BorderBrush="#FF000000">
<StackPanel x:Name="EditPersonStackPanel" Orientation="Vertical" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center" Width="650" >
<ContentControl>
<StackPanel x:Name="EmailEditStackPanel" Orientation="Horizontal">
<TextBlock Text="Email Address:" Width="200" Margin="5,0,5,0" />
<TextBox x:Name="EmailPopupTextBox" Width="200" />
</StackPanel>
</ContentControl>
<ContentControl>
<StackPanel x:Name="AppliesToDropdownStackPanel" Orientation="Horizontal" Margin="2,2,2,0">
<TextBlock Text="Don't send when update was done by:" />
<StackPanel Orientation="Vertical" MaxHeight="275" MaxWidth="350" >
<TextBlock x:Name="SelectedItemTextBlock" TextAlignment="Right" Width="200" Margin="5,0,5,0" />
<Grid x:Name="UserDropDownGrid" MaxHeight="75" MaxWidth="200" Visibility="Collapsed" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Hidden" >
<Rectangle Fill="White" />
<Border Background="White">
<ListBox x:Name="UsersListBox" SelectionChanged="UsersListBox_SelectionChanged" ItemsSource="{Binding UserID}" />
</Border>
</Grid>
</StackPanel>
<Button x:Name="DropDownButton" Click="DropDownButton_Click" VerticalAlignment="Top" Width="25" Height="25">
<Path Height="10" Width="10" Fill="#FF000000" Stretch="Fill" Stroke="#FF000000" Data="M514.66669,354 L542.16669,354 L527.74988,368.41684 z" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1,1,1,1"/>
</Button>
</StackPanel>
</ContentControl>
<TextBlock Text="Where Clause Condition:" />
<TextBox x:Name="WhereClauseTextBox" Height="200" Width="800" AcceptsReturn="True" TextWrapping="Wrap" />
<ContentControl>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button x:Name="TestConditionButton" Content="Test Condition" Margin="5,5,5,5" Click="TestConditionButton_Click" />
<Button x:Name="Save" Content="Save" HorizontalAlignment="Right" Margin="5,5,5,5" Click="Save_Click" />
<Button x:Name="Cancel" Content="Cancel" HorizontalAlignment="Right" Margin="5,5,5,5" Click="Cancel_Click" />
</StackPanel>
<TextBlock x:Name="TestContitionResults" Visibility="Collapsed" />
</StackPanel>
</ContentControl>
</StackPanel>
</Border>
</Grid>
</Grid>
And the call that occurs when the grid's selection is changed:
Private Sub IncidentGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If mFirstTime Then
mFirstTime = False
Else
Dim data As SimpleASMX.EMailMonitor = CType(IncidentGrid.SelectedItem, SimpleASMX.EMailMonitor)
Dim selectedGridItem As SimpleASMX.EMailMonitor = Nothing
If IncidentGrid.SelectedItem IsNot Nothing Then
selectedGridItem = CType(IncidentGrid.SelectedItem, SimpleASMX.EMailMonitor)
EmailPopupTextBox.Text = selectedGridItem.Address
SelectedItemTextBlock.Text = selectedGridItem.AppliesToUserID
WhereClauseTextBox.Text = selectedGridItem.whereClause
IncidentGrid.SelectedIndex = mEmailMonitorData.IndexOf(selectedGridItem)
End If
If IncidentGrid.SelectedIndex > -1 Then
EditPersonPopupGrid.Visibility = Windows.Visibility.Visible
Else
EditPersonPopupGrid.Visibility = Windows.Visibility.Collapsed
End If
End If
End Sub
Sorry if my code is atrocious, I'm still learning Silverlight.
That looks like a Silverlight bug to me. I've just tried it and what's happening on my end is that the SelectionChanged event fires twice when you click the column header and to make matters worse, the index of the selected item doesn't stay synched with the currently selected item.
I'd suggest you work your way around it by using the knowledge that the first time SelectionChanged fires, the value of the datagrid's SelectedItem property will be null
Here's some sample code that at least lives with the issue. Your SelectionChanged logic can go in the if clause.
public partial class Page : UserControl
{
private Person _currentSelectedPerson;
public Page()
{
InitializeComponent();
List<Person> persons = new List<Person>();
persons.Add(new Person() { Age = 5, Name = "Tom" });
persons.Add(new Person() { Age = 3, Name = "Lisa" });
persons.Add(new Person() { Age = 4, Name = "Sam" });
dg.ItemsSource = persons;
}
private void SelectionChanged(object sender, EventArgs e)
{
DataGrid grid = sender as DataGrid;
if (grid.SelectedItem != null)
{
_currentSelectedPerson = grid.SelectedItem as Person;
}
else
{
grid.SelectedItem = _currentSelectedPerson;
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Frozen Columns in Silverlight DataGrid..
http://dotnetdreamer.wordpress.com/2009/01/31/silverlight-2-datagrid-frozen-columns/
There's a bugfix for the first issue you mentioned (selection changed event getting fired on resort).
See the following URL for Microsoft's patch:
http://www.microsoft.com/downloads/details.aspx?familyid=084A1BB2-0078-4009-94EE-E659C6409DB0&displaylang=en
This worked, but now if I sort twice, on the first one it sorts, and then does the popup as the first selected item of the grid . If I close the popup grid, and then try to sort a second time, it stack overflows, and crashes firefox out.
I'm thinking I may need to rethink working in silverlight until the system gets a bit more stable.
Thanks for the answer Hovito!