Set Tooltip Image in Code for Listbox Items - wpf

I have a class ExpanderItems which gets loaded during runtime and a list of these is set as the DataContext of a ListBox. Now what I want to do is show the corresponding Images as a Tooltip for each Item. Any suggestions how to do that?
public class ExpanderItem
{
private String mItemName = "empty";
public String ItemName
{
get { return mItemName; }
set { mItemName = value; }
}
private Image mItemSymbol = null;
public Image ItemSymbol
{
get { return mItemSymbol; }
set { mItemSymbol = value; }
}
}
public List<ExpanderItem> getExpanderItems()
{
List<ExpanderItem> ItemList = new List<ExpanderItem>();
ExpanderItem i0 = new ExpanderItem();
i0.ItemName = "Constant";
i0.ItemSymbol = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(#"/resources/Constant.png", UriKind.RelativeOrAbsolute);
bi.EndInit();
i0.ItemSymbol.Source = bi;
ItemList.Add(i0);
...
}
In the Window where the Items are used I am calling:
void WindowMain_Loaded(object sender, RoutedEventArgs e)
{
lbItems.DataContext = SomeService.getExpanderItems();
}
XAML Looks like:
<ListBox x:Name="lstItems" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding ItemName}">
</Label>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Compiled and tested solution.
XAML:
<ListBox x:Name="lb"
ItemsSource="{Binding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ToolTip">
<Setter.Value>
<Image Stretch="UniformToFill"
Source="{Binding ItemSymbol}" />
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Label Grid.Column="1"
Content="{Binding ItemName}">
</Label>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
namespace Test
{
public class ExpanderItem
{
private String mItemName = "empty";
public String ItemName
{
get { return mItemName; }
set { mItemName = value; }
}
private BitmapImage mItemSymbol = null;
public BitmapImage ItemSymbol
{
get { return mItemSymbol; }
set { mItemSymbol = value; }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
lb.DataContext = this.getExpanderItems();
}
public List<ExpanderItem> getExpanderItems()
{
List<ExpanderItem> ItemList = new List<ExpanderItem>();
ExpanderItem i0 = new ExpanderItem
{
ItemName = "Constant",
ItemSymbol = new BitmapImage(new Uri(#"/resources/constant.png", UriKind.RelativeOrAbsolute))
};
ItemList.Add(i0);
ExpanderItem i1 = new ExpanderItem
{
ItemName = "Constant",
ItemSymbol = new BitmapImage(new Uri(#"/resources/constant.png", UriKind.RelativeOrAbsolute))
};
ItemList.Add(i1);
return ItemList;
}
}
}

I found a solution like this, but this does not add the tooltip to the whole listbox item but just to the textbox of it.
public class ExpanderItem
{
private String mItemName = "empty";
public String ItemName
{
get { return mItemName; }
set { mItemName = value; }
}
private Image mItemSymbol = null;
public Image ItemSymbol
{
get { return mItemSymbol; }
set { mItemSymbol = value; }
}
}
public List<ExpanderItem> getExpanderItems()
{
List<ExpanderItem> ItemList = new List<ExpanderItem>();
ExpanderItem i0 = new ExpanderItem();
i0.ItemName = "Constant";
i0.ItemSymbol = new Uri(#"/resources/Constant.png", UriKind.RelativeOrAbsolute);
ItemList.Add(i0);
...
}
XAML:
<ListBox.ItemTemplate>
<DataTemplate>
...
<Label Grid.Column="1" Content="{Binding ItemName}">
<Label.ToolTip>
<Border BorderBrush="Black"
BorderThickness="1"
CornerRadius="2">
<Image Source="{Binding ItemSymbol}" />
</Border>
</Label.ToolTip>
</Label>

Related

Tabs not showing when adding them through method

I have a datagrid. I want to add Tab when row double click event is rised. When AddTab method is called Tab is added to ObservableCollection, but it doesn't show up on TabControl. Why it doesn't show up? Because there is a wrong DataContext when method called?
<DataGrid helpers:RowDoubleClickHandler.MethodName="AddTab" AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="True" CanUserResizeRows="True" x:Name="dataGrid1"
ItemsSource="{Binding DataGridEntries3}" HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" IsReadOnly="True">
AddTab method in ViewModel
public void AddTab()
{
Tabs.Add(new TabEntry
{
Description = "Tab3",
DataGridEntries = new ObservableCollection<DataGridEntry>()
{
new DataGridEntry()
{
}
}
});
XAML with TabControl
<Window x:Class="ProjectZero.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ProjectZero"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<vm:MainViewModel x:Key="ViewModel"/>
</Window.Resources>
<Grid>
<ToolBar Height="40" VerticalAlignment="Top">
<Menu>
<MenuItem Header="Menu" Margin="6" Foreground="White" FontSize="14" FontFamily="Times New Roman">
<MenuItem Header="Add Invoice" Command="{Binding AddInvoice}"/>
<MenuItem Header="Invoices List" Command="{Binding AddInvoiceList}" FontFamily="Tahoma" />
</MenuItem>
<Menu.Background>
<SolidColorBrush />
</Menu.Background>
</Menu>
<ToolBar.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FF173ADE" Offset="0.431" />
<GradientStop Color="#FF0B1D6F" Offset="0.646" />
</LinearGradientBrush>
</ToolBar.Background>
</ToolBar>
<TabControl x:Name="tabControl1" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding Tabs}" ItemTemplate="{DynamicResource DataTemplateType1}" TabStripPlacement="Top" HorizontalAlignment="Stretch" Margin="10,46,0,0" VerticalAlignment="Stretch" Width="Auto">
</TabControl>
ViewModel for Tabs
public class MainViewModel : BaseViewModel
{
public RelayCommand RelayCommand { get; set; }
public MainViewModel()
{
this.RelayCommand = new RelayCommand(this);
Tabs.CollectionChanged += (o, e) =>
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (BaseViewModel item in e.NewItems)
if (item.MyType == "TabEntry")
DataGridEntries3.Add(item as TabEntry);
}
};
var t = (from i in Tabs where i.MyType == "TabEntry" select (TabEntry)i);
DataGridEntries3 = new ObservableCollection<TabEntry>(t);
}
private ObservableCollection<BaseViewModel> _tabs;
public ObservableCollection<BaseViewModel> Tabs
{
get { return _tabs != null ? _tabs : _tabs = new ObservableCollection<BaseViewModel>(); }
set { _tabs = value; OnPropertyChanged("Tabs"); }
}
BaseViewModel _SelectedItem;
public BaseViewModel SelectedItem
{
get { return _SelectedItem; }
set { _SelectedItem = value; OnPropertyChanged("SelectedItem"); }
}
private ObservableCollection<TabEntry> _DataGridEntries3;
public ObservableCollection<TabEntry> DataGridEntries3
{
get { return _DataGridEntries3 != null ? _DataGridEntries3 : _DataGridEntries3 = new ObservableCollection<TabEntry>(); }
set { _DataGridEntries3 = value; OnPropertyChanged("DataGridEntries3"); }
}
ICommand _AddInvoice = null;
ICommand _AddInvoiceList = null;
public ICommand AddInvoice
{
get
{
return _AddInvoice != null ? _AddInvoice : _AddInvoice.SetCommand(param =>
{
Tabs.Add(new TabEntry
{
Description = "Tab3",
DataGridEntries = new ObservableCollection<DataGridEntry>()
{
new DataGridEntry()
{
}
}
});
});
}
}
public ICommand AddInvoiceList
{
get
{
return _AddInvoiceList != null ? _AddInvoiceList : _AddInvoiceList.SetCommand(param =>
{
var tab_dc = Tabs.FirstOrDefault(it => it.GetType() == typeof(MainViewModel));
if (tab_dc != null)
{
Tabs.Add(tab_dc);
}
else
{
var new_tab = new MainViewModel();
Tabs.Add(new_tab);
}
});
}
}
}
Also here is RowDoubleClick Event Handler
public sealed class RowDoubleClickHandler : FrameworkElement
{
public RowDoubleClickHandler(DataGrid dataGrid)
{
MouseButtonEventHandler handler = (sender, args) =>
{
var row = sender as DataGridRow;
if (row != null && row.IsSelected)
{
var methodName = GetMethodName(dataGrid);
var dataContextType = dataGrid.DataContext.GetType();
var method = dataContextType.GetMethod(methodName);
if (method == null)
{
throw new MissingMethodException(methodName);
}
method.Invoke(dataGrid.DataContext, null);
}
};
dataGrid.LoadingRow += (s, e) =>
{
e.Row.MouseDoubleClick += handler;
};
dataGrid.UnloadingRow += (s, e) =>
{
e.Row.MouseDoubleClick -= handler;
};
}
public static string GetMethodName(DataGrid dataGrid)
{
return (string)dataGrid.GetValue(MethodNameProperty);
}
public static void SetMethodName(DataGrid dataGrid, string value)
{
dataGrid.SetValue(MethodNameProperty, value);
}
public static readonly DependencyProperty MethodNameProperty = DependencyProperty.RegisterAttached(
"MethodName",
typeof(string),
typeof(RowDoubleClickHandler),
new PropertyMetadata((o, e) =>
{
var dataGrid = o as DataGrid;
if (dataGrid != null)
{
new RowDoubleClickHandler(dataGrid);
}
}));
}
A few tips, since you asked in your comment:
This:
private ObservableCollection<BaseViewModel> _tabs;
public ObservableCollection<BaseViewModel> Tabs
{
get { return _tabs != null ? _tabs : _tabs = new ObservableCollection<BaseViewModel>(); }
set { _tabs = value; OnPropertyChanged("Tabs"); }
}
Could be rewritten a lot cleaner as:
public ObservableCollection<BaseViewModel> Tabs { get; private set; }
With this added to your constructor:
Tabs = new ObservableCollection<BaseViewModel>();
This is a consistent theme in your code. Basically, don't make modifications in your property getters/setters unless you really really need to. And in your case, all it is doing is complicating things.
It is especially confusing when you have a getter that then will add a tab to your collection of tabs when first invoked. That is very difficult to follow programmatically. Just do your initialization in your constructor like everyone else does :)

How to use Binding with a Binding in it

Hi following suggestion
you have a Textblock which DataContext is:
this[0]
this[1]
this[...]
this[n]
this Textblock is a child of a DatagridCell
now i want to get set a Binding based on the Column position
so i wrote Binding RelativeSource={RelativeSource FindAncestor,AncestorType=DataGridCell},Path=Column.DisplayIndex } which works fine
to get a this[...] i need to bind like Binding Path=[0] which also works well
but if i but both together like this:
{ Binding Path=[ {Binding Path=Column.DisplayIndex, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridCell}} ] }
it doesn't bind here the Error
System.Windows.Data Error: 40 : BindingExpression path error: '[]' property not found on 'object' ''List`1' (HashCode=33153114)'. BindingExpression:Path=[{Binding Path=Column.DisplayIndex, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridCell} }]; DataItem='List`1' (HashCode=33153114); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
so does anyone know how to do this?
Edit:
here simple code:
XAML
<DataGrid AutoGenerateColumns="true" Height="200" HorizontalAlignment="Left" Margin="243,12,0,0" Name="grid" VerticalAlignment="Top" Width="200"
SelectionMode="Extended" SelectionUnit="Cell">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<StackPanel >
<TextBlock Text="{Binding Path=Column.DisplayIndex, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridCell} }" />
<TextBlock Text="{Binding Path=[0] }" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
CS
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var gridList = new List<List<MyCell>>();
for (int i = 0; i < 5; i++)
{
var cell1 = new MyCell { Text1 = "nr " + i, Text2 = "value1" };
var cell2 = new MyCell { Text1 = "nr " + i, Text2 = "value2" };
var sublist = new List<MyCell>();
sublist.Add(cell1);
sublist.Add(cell2);
gridList.Add(sublist);
}
grid.ItemsSource = gridList;
}
}
public class MyCell
{
string value1;
string value2;
public string Text1
{
get { return value1; }
set { value1 = value; }
}
public string Text2
{
get { return value2; }
set { value2 = value; }
}
}
Interesting constillation you got there.
It is very well possible to do what you actually asking for.
The Binding Path has a property called PathParameters and here is how you can use it:
new Binding {
Path = new PropertyPath("Values[(0)]", new DateTime(2011, 01, 01))
}
In this example instead of zero the date is gonna be injected.
You will find here about path syntax:
http://msdn.microsoft.com/en-us/library/ms742451.aspx
Edit 2:
Hack wpf to make it work.
Lets say this is your ViewModel.
public class VM
{
private Dictionary<int, string> dic;
public Dictionary<int, string> Dic
{
get
{
if (dic == null)
{
dic = new Dictionary<int, string>();
dic[123] = "Hello";
}
return dic;
}
}
public int Index
{
get
{
return 123;
}
}
}
This is XAML:
I am having DataContext inside Resources as you can see.
<Window x:Class="WpfApplication1.MainWindow"
xmlns:helper="clr-namespace:WpfApplication1.Helper">
<Window.Resources>
<local:VM x:Key="viewModel"/>
</Window.Resources>
<StackPanel>
<Button>
<Button.Content>
<helper:ParameterBinding Source="{StaticResource viewModel}" PropertyName="Dic" HasIndex="True">
<helper:ParameterBinding.ParameterObject>
<helper:ParameterBindingHelperObject BindableParameter="{Binding Source={StaticResource viewModel}, Path=Index}"/>
</helper:ParameterBinding.ParameterObject>
</helper:ParameterBinding>
</Button.Content>
</Button>
</StackPanel>
</Window>
And this is the key to everything:
public class ParameterBinding : Binding
{
private ParameterBindingHelperObject parameterObject;
public ParameterBindingHelperObject ParameterObject
{
get
{
return parameterObject;
}
set
{
this.parameterObject = value;
this.parameterObject.Binding = this;
}
}
public bool HasIndex
{
get;
set;
}
public string PropertyName
{
get;
set;
}
public void UpdateBindingPath()
{
string path = this.PropertyName + (HasIndex ? "[" : "") + this.ParameterObject.BindableParameter + (HasIndex ? "]" : "");
this.Path = new PropertyPath(path);
}
}
public class ParameterBindingHelperObject : DependencyObject
{
private ParameterBinding binding;
public ParameterBinding Binding
{
get
{
return binding;
}
set
{
this.binding = value;
this.binding.UpdateBindingPath();
}
}
public object BindableParameter
{
get { return (object)GetValue(BindableParameterProperty); }
set { SetValue(BindableParameterProperty, value); }
}
public static readonly DependencyProperty BindableParameterProperty =
DependencyProperty.Register("BindableParameter", typeof(object), typeof(ParameterBindingHelperObject), new UIPropertyMetadata(null, PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
ParameterBindingHelperObject obj = (ParameterBindingHelperObject)dependencyObject;
if (obj.Binding != null)
{
obj.Binding.UpdateBindingPath();
}
}
}
I inherit from Binding and place a bindable property which will serve as parameter.
Technically you can change this and make most awesome binding ever. You could allow to change index number at runtime and path will change.
What you think of this?

Binding SetectedItem in ComboBox WPF

I have a UserControl with ComboBox.
<ComboBox Grid.Row="8" Grid.Column="1"
VerticalAlignment="Center"
x:Name="cmbCategory"
ItemsSource="{Binding ElementName=ucAppiGeneralInfo, Path=Categories, Mode=TwoWay}"
SelectedItem="{Binding ElementName=ucAppiGeneralInfo, Path=SelectedCategory, Mode=TwoWay}"
IsEditable="True"
IsSynchronizedWithCurrentItem="True"
SelectedValuePath="CAT_ID"
TextSearch.TextPath="CAT_NAME">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=CAT_NAME}"/>
<TextBlock Text=" - "/>
<TextBlock Text="{Binding Path=PUBLIC_DESCRIPTION}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The code behind is:
public partial class AppiGeneralInfoUC : UserControl
{
public DataTable Categories
{
get { return (DataTable)GetValue(CategoriesProperty); }
set { SetValue(CategoriesProperty, value);}
}
public static readonly DependencyProperty CategoriesProperty =
DependencyProperty.Register(
"Categories",
typeof(DataTable),
typeof(AppiGeneralInfoUC),
new UIPropertyMetadata(null));
public String SelectedCategory
{
get { return (String)GetValue(SelectedCategoryProperty); }
set
{
SetValue(SelectedCategoryProperty, value);
}
}
public static readonly DependencyProperty SelectedCategoryProperty =
DependencyProperty.Register(
"SelectedCategory",
typeof(String),
typeof(AppiGeneralInfoUC),
new UIPropertyMetadata(null));
public AppiGeneralInfoUC()
{
InitializeComponent();
}
}
I have a window which use the UserControl:
<TabControl>
<TabItem Header="Information">
<my:AppiGeneralInfoUC x:Name="ucAppiGeneralInfo"
Categories="{Binding Path=Categories, Mode=TwoWay}"
SelectedCategory="{Binding Path=SelectedCategory, Mode=TwoWay}" />
</TabItem>
the code behind is:
public partial class ApplicationWindow : Window
{
VMBase appiGeneralInfoWin = new AppiGeneralInfoVM();
public ApplicationWindow()
{
InitializeComponent();
ucAppiGeneralInfo.DataContext = appiGeneralInfoWin;
}
public void updateAction(string cat_id)
{
this.Title = "Update application";
(appiGeneralInfoWin as AppiGeneralInfoVM).setSelectedCategory(cat_id);
} ...
And finally I have ViewModel class:
class AppiGeneralInfoVM : VMBase
{
private DataTable categories = null;
private String selectedCategory = null;
public DataTable Categories
{
get { return this.categories; }
set
{
this.categories = value;
this.OnPropertyChanged("Categories");
}
}
public String SelectedCategory
{
get { return this.selectedCategory; }
set
{
this.selectedCategory = value;
this.OnPropertyChanged("SelectedCategory");
}
}
public AppiGeneralInfoVM()
{
ServicesLoader.LoadRunTimeServices();
Categories = GetService<CategoryBLL>().getCategories();
}
public void setSelectedCategory(string cat_id)
{
SelectedCategory = Categories.Select("cat_id =" + "'"+cat_id+"'")[0]["CAT_NAME"].ToString();
}
Everything works well but i have problem with the selectedItem (SelectedCategory),
it's not update at all....
I think it happens because your SelectedItem has string type while your collection is DataTable (which enumerates DataRow). Try changing your collection to be IEnumerable<string>

WPF binding Dictionary<string, List<string> to listView, ListBox how?

how to bind Dictionary to ListView and Text box?
namespace Models
{
public class DynamicList
{
#region Constructor
public DynamicList()
{
_propDict = new Dictionary<string, object>();
dynimicListProps = new List<DynimicListProperty>();
}
#endregion Constructor
#region Fields
private Dictionary<string, object> _propDict;
private IList<DynimicListProperty> dynimicListProps;
#endregion Fields
#region Properties
public Dictionary<string, object> PropsDict
{
get { return _propDict; }
set { _propDict = value; }
}
public string TestString
{
get { return "Hello! It's works!"; }
}
#endregion Properties
#region Methods
public void CreateProperties(string[] arrLine)
{
for (int i = 0; i < arrLine.Count(); i++)
{
_propDict.Add(arrLine[i].Replace("\"",""), null);
}
}
#endregion Methods
}
public class DynimicListProperty
{
private IList<string> propertyNameValues = new List<string>();
public IList<string> PropertyNameValues
{
get { return propertyNameValues; }
set { propertyNameValues = value; }
}
}
}
// now try to bind
private Models.DynamicList _dynimicList;
public Models.DynamicList _DynimicList
{
get { return _dynimicList; }
}
CreateView()
{
_importView = new Views.ImportBomView();
_importView.Grid1.DataContext = _DynimicList;
Binding bn = new Binding("Value.[2]");
bn.Mode = BindingMode.OneWay;
bn.Source = _DynimicList.PropsDict.Keys;
_importView.tbFileName.SetBinding(TextBlock.TextProperty, bn);
/////////////////////////////////////////////////////////////////////////////
//_importView.listView1.ItemsSource = (IEnumerable)_DynimicList.PropsDict["Value"];
////// it's works when Binding bn2 = new Binding("") but of course in
///this emplementation I have the same data in all columns - so not good
/////////////////////////////////////////////////////////////////////////////////////
// here I'll like to generate Columns and bind gvc.DisplayMemberBinding
// to dictionary _DynimicList.PropsDict[item] with Key=item
foreach (var item in _DynimicList.PropsDict.Keys)
{
Binding bn2 = new Binding("[3]");
bn2.Source = (IEnumerable)_DynimicList.PropsDict[item];
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = bn2;
gvc.Header = item;
gvc.Width = 100;
_importView.gridView1.Columns.Add(gvc);
}
_importView.Show();
}
}
You can write a datatemplate for KeyValuePair<string, List<string>> and put it in the ItemsTemplate of the root ListView. The ItemsSource of your ListView would be your dictionary. In this datatemplate you would have another itemscontrol (such as another listview) where you set the itemstemplate to a textbox that binds to the string. Alternatively you could use a single TreeView for everything in conjunction with hierarchical datatemplate. You can make a TreeView look however you want using templates.
<ListBox Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{Binding Key}" Margin="0 0 4 0"/>
<ItemsControl ItemsSource="{Binding Value}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Margin" Value="0 0 2 0" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And in the code behind some sample data:
var data = new Dictionary<string, List<string>>
{
{"1", new List<string> {"one", "two", "three", "four"}},
{"2", new List<string> {"five", "six", "seven", "eight"}}
};
this.listBox.ItemsSource = data;

WPF List View Sort on Load

Here is the problem:
I want to sort a ListView when it is first loaded.
I have implemented the functionality where in the List View can be sorted if the Header columns
in the ListView are clicked.
I unable to find a suitable event which I can use to call my sort function.
I tried using OnInitialized of the UserControl and Loaded events but it seems the List View is
not populated when I call these functions.
I tried GotFocus of ListView. It works but then I have to click on the window to get the sorting done.
I want the sorting to be done as soon as the ListView is loaded.
I am using XML data binding with the List View.
The ListView is part of a UserControl. The User Control is hosted in a MMC app.
Please let me know if you need any other information.
public class SortableGridViewColumn : GridViewColumn
{
public string SortPropertyName
{
get { return (string)GetValue(SortPropertyNameProperty); }
set { SetValue(SortPropertyNameProperty, value); }
}
// Using a DependencyProperty as the backing store for SortPropertyName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SortPropertyNameProperty =
DependencyProperty.Register("SortPropertyName", typeof(string),
typeof(SortableGridViewColumn), new UIPropertyMetadata(""));
public bool IsDefaultSortColumn
{
get { return (bool)GetValue(IsDefaultSortColumnProperty); }
set { SetValue(IsDefaultSortColumnProperty, value); }
}
public static readonly DependencyProperty IsDefaultSortColumnProperty =
DependencyProperty.Register("IsDefaultSortColumn", typeof(bool),
typeof(SortableGridViewColumn), new UIPropertyMetadata(false));
}
public class SortableListView : ListView
{
public SortableListView()
{
}
SortableGridViewColumn lastSortedOnColumn = null;
ListSortDirection lastDirection = ListSortDirection.Ascending;
public void Sort(string sortBy, ListSortDirection direction)
{
ICollectionView dataView = CollectionViewSource.GetDefaultView
(this.ItemsSource);
//Check if dataView isn't null
if (dataView != null)
{
dataView.SortDescriptions.Clear();
SortDescription sd1 = new SortDescription("#isenabled", direction);
dataView.SortDescriptions.Add(sd1);
SortDescription sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
}
private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
if (headerClicked != null &&
headerClicked.Role != GridViewColumnHeaderRole.Padding)
{
// attempt to cast to the sortableGridViewColumn object.
SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;
// ensure that the column header is the correct type and a sort property has been set.
if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
{
ListSortDirection direction;
bool newSortColumn = false;
// determine if this is a new sort, or a switch in sort direction.
if (lastSortedOnColumn == null
|| String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName)
|| !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
{
newSortColumn = true;
direction = ListSortDirection.Ascending;
}
else
{
if (lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
}
// get the sort property name from the column's information.
string sortPropertyName = sortableGridViewColumn.SortPropertyName;
// Sort the data.
Sort(sortPropertyName, direction);
lastSortedOnColumn = sortableGridViewColumn;
lastDirection = direction;
}
}
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
// add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView.
this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClickedHandler));
// cast the ListView's View to a GridView
GridView gridView = this.View as GridView;
if (gridView != null)
{
// determine which column is marked as IsDefaultSortColumn. Stops on the first column marked this way.1
SortableGridViewColumn sortableGridViewColumn = null;
foreach (GridViewColumn gridViewColumn in gridView.Columns)
{
sortableGridViewColumn = gridViewColumn as SortableGridViewColumn;
if (sortableGridViewColumn != null)
{
if (sortableGridViewColumn.IsDefaultSortColumn)
{
break;
}
sortableGridViewColumn = null;
}
}
// if the default sort column is defined, sort the data
if (sortableGridViewColumn != null)
{
lastSortedOnColumn = sortableGridViewColumn;
Sort(sortableGridViewColumn.SortPropertyName, ListSortDirection.Ascending);
}
}
}
}
The XAML is as shown below:
**<local:SortableListView x:Name="ListViewControl" Grid.Row="0" ItemContainerStyle="{DynamicResource StretchedContainerStyle}"
ItemTemplateSelector="{DynamicResource myControlTemplateSelector}"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource dataProvider},
XPath=//CONFIGURATION}">
<ListView.View >
<GridView >
<local:SortableGridViewColumn Header="ID" HeaderContainerStyle="{StaticResource CustomHeaderStyle}"
DisplayMemberBinding="{Binding XPath=./#id}"
IsDefaultSortColumn="True"
SortPropertyName="#id"/>
<local:SortableGridViewColumn Header="VALUE" HeaderContainerStyle="{StaticResource CustomHeaderStyle}"
CellTemplateSelector="{DynamicResource myControlTemplateSelector}"
SortPropertyName="#value"/>
<local:SortableGridViewColumn Header="DATATYPE" HeaderContainerStyle="{StaticResource CustomHeaderStyle}"
DisplayMemberBinding="{Binding XPath=./#data_type}"
SortPropertyName="#data_type"/>
<local:SortableGridViewColumn Header="DESCRIPTION" HeaderContainerStyle="{StaticResource CustomHeaderStyle}"
DisplayMemberBinding="{Binding XPath=./#description}"
SortPropertyName="#description"
Width="{Binding ElementName=ListViewControl, Path=ActualWidth}"/>
</GridView>
</ListView.View>
</local:SortableListView>**
<StackPanel Grid.Row="1">
<Button Grid.Row="1" HorizontalAlignment="Stretch" Height="34" HorizontalContentAlignment="Stretch" >
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Center" Orientation="Horizontal" FlowDirection="RightToLeft" Height="30">
<Button Grid.Row="1" Content ="Apply" Padding="0,0,0,0 " Margin="6,2,0,2" Name="btn_Apply" HorizontalAlignment="Right" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="132" IsTabStop="True" Click="btn_ApplyClick" Height="24" />
</StackPanel >
</Button>
</StackPanel >
</Grid>
I finally was able to resolve it.
I had to use the Converter on ListView ItemSource. And then sort the List on Convert Function.
Here is the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Threading;
using System.Collections.ObjectModel;
namespace ...MiscellaneousCAESettings
{
/// <summary>
/// Interaction logic for ConfigDataView.xaml
/// </summary>
public partial class ConfigDataView : UserControl, IConfigDataViewControl
{
ConfigDataViewPresenter _presenter = null;
public static string _currDataType = "";
public static string _min = "" ;
public static string _max = "";
public string Min
{
get
{
return _min;
}
set
{
_min = value ;
}
}
public string Max
{
get
{
return _max;
}
set
{
_max = value;
}
}
public string CurrDataType
{
get
{
return _currDataType;
}
set
{
_currDataType = value;
}
}
public ConfigDataView()
{
InitializeComponent();
//To give the classic windows look
Uri uri = new Uri("PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component\\themes/classic.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);
}
private void txtBoxGotFocus(object sender, RoutedEventArgs e)
{
Min = "" ;
Max = "" ;
TextBox txtbox = e.Source as TextBox;
this.ListViewControl.SelectedItem = txtbox.DataContext;
//index
int index = this.ListViewControl.Items.IndexOf(this.ListViewControl.SelectedItem);
System.ComponentModel.ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ListViewControl.ItemsSource);
object stCurr = (dataView.CurrentPosition ) ;
//Check if the "data_type" attribute exists
if (((XmlElement)dataView.CurrentItem).Attributes["data_type"] != null)
{
CurrDataType = ((XmlElement)dataView.CurrentItem).Attributes["data_type"].Value;
}
//Check if the "min" attribute exists
if (((XmlElement)dataView.CurrentItem).Attributes["min"] != null)
{
Min = ((XmlElement)dataView.CurrentItem).Attributes["min"].Value;
}
//Check if the "min" attribute exists
if (((XmlElement)dataView.CurrentItem).Attributes["max"] != null)
{
Max = ((XmlElement)dataView.CurrentItem).Attributes["max"].Value;
}
}
#region IConfigDataViewControl Members
public void LoadRootConfigData(string xmlFileName, string xmlFileContent, string xmlXPath)
{
try
{
XmlDocument configFileDoc = new XmlDocument();
configFileDoc.LoadXml(xmlFileContent);
XmlDataProvider xmldp = (XmlDataProvider)this.TryFindResource("dataProvider");
xmldp.Document = configFileDoc;
if (string.IsNullOrEmpty(xmlXPath))
{
xmldp.XPath = #"//node()[1]/node()[#value]";
}
else
{
xmldp.XPath = xmlXPath;
}
Binding bnd = new Binding();
bnd.Source = xmldp;
bnd.Converter = new SortList();
ListViewControl.SetBinding(ItemsControl.ItemsSourceProperty, bnd);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void LoadCategoryConfigData(string xmlFile, string xmlFileContent, string CategoryNodeName)
{
try
{
XmlDocument configFileDoc = new XmlDocument();
configFileDoc.LoadXml(xmlFileContent);
XmlDataProvider xmldp = (XmlDataProvider)this.TryFindResource("dataProvider");
xmldp.Document = configFileDoc;
xmldp.XPath = #"//CONTEXT[#id='" + CategoryNodeName + #"']/CONFIGURATION";
Binding bnd = new Binding();
bnd.Source = xmldp;
bnd.Converter = new SortList();
ListViewControl.SetBinding(ItemsControl.ItemsSourceProperty, bnd);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void AttachPresenter(ConfigDataViewPresenter cfgpresenter)
{
_presenter = cfgpresenter;
}
#endregion
private void btn_ApplyClick(object sender, RoutedEventArgs e)
{
XmlDataProvider odp = (XmlDataProvider)this.TryFindResource("dataProvider");
XmlDocument configFileDoc = new XmlDocument();
configFileDoc =odp.Document;
_presenter.Save(configFileDoc.InnerXml );
}
}
public class TextBoxMinMaxValidation : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
try
{
//Check for min max string length if it is a "Text" data type
if (ConfigDataView._currDataType.ToLower() == "text")
{
int minLength = Convert.ToInt32(ConfigDataView._min);
int maxLength = Convert.ToInt32(ConfigDataView._max);
int strLength = value.ToString().Length;
bool isValidLength = true;
isValidLength = ((strLength >= minLength) && (strLength <= maxLength));
if (!isValidLength)
{
return new ValidationResult(false, string.Format("The input String Length is out of range. The String Length should be between {0} to {1}", minLength, maxLength));
}
else
{
return new ValidationResult(true, null);
}
}
//Check for min max string length if it is a "Numeric" data type
if (ConfigDataView._currDataType.ToLower() != "numeric")
{
return new ValidationResult(true, null);
}
int min = Convert.ToInt32(ConfigDataView._min);
int max = Convert.ToInt32(ConfigDataView._max);
int res ;
bool isNumber = int.TryParse(value.ToString(), out res);
bool isValidRange = true;
if (!isNumber)
{
return new ValidationResult(false, "The input string is in incorrect format. Should be a Number.");
}
isValidRange = ((res >= min) && (res <= max));
if (!isValidRange)
{
return new ValidationResult(false, string.Format("The input integer is out of range. The number should be between {0} to {1}", min, max));
}
}
catch
{
}
return new ValidationResult(true, null);
}
}
public class ControlTemplateSelector : DataTemplateSelector
{
public const String XML_TAG_DATATYPE = "data_type";
public const String DATATYPE_DROPDOWN = "Dropdown";
public const String DATATYPE_BOOLEAN = "Boolean";
public override DataTemplate SelectTemplate(object item,
DependencyObject container)
{
FrameworkElement window = (container as FrameworkElement);
try
{
XmlNode node = (XmlNode)item;
String dataType = "";
if (node.Attributes[XML_TAG_DATATYPE] != null)
{
dataType = (string)node.Attributes.GetNamedItem(XML_TAG_DATATYPE).Value;
}
if (dataType == DATATYPE_DROPDOWN)
{
return window.FindResource("dropDownTemplate") as DataTemplate;
}
if (dataType == DATATYPE_BOOLEAN)
{
return window.FindResource("booldropDownTemplate") as DataTemplate;
}
}
catch (Exception ex)
{
MessageBox.Show("Select template Exception" + ex.Message );
}
return window.FindResource("textTemplate") as DataTemplate;
}
}
public class boolConverter : IValueConverter
{
public const String XML_TAG_VALUE = "value";
public const String XML_TAG_ID = "id";
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Boolean boolVal = false;
try
{
boolVal = System.Convert.ToBoolean(value);
}
catch
{
string strVal = value.ToString();
int iVal = int.Parse(strVal);
boolVal = System.Convert.ToBoolean(iVal);
}
if (boolVal == true)
{
return 1;
}
else
{
return 0;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Boolean boolVal = false;
try
{
boolVal = System.Convert.ToBoolean(value);
}
catch
{
string strVal = value.ToString();
int iVal = int.Parse(strVal);
boolVal = System.Convert.ToBoolean(iVal);
}
return boolVal;
}
}
public class SortableGridViewColumn : GridViewColumn
{
public string SortPropertyName
{
get { return (string)GetValue(SortPropertyNameProperty); }
set { SetValue(SortPropertyNameProperty, value); }
}
// Using a DependencyProperty as the backing store for SortPropertyName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SortPropertyNameProperty =
DependencyProperty.Register("SortPropertyName", typeof(string),
typeof(SortableGridViewColumn), new UIPropertyMetadata(""));
public bool IsDefaultSortColumn
{
get { return (bool)GetValue(IsDefaultSortColumnProperty); }
set { SetValue(IsDefaultSortColumnProperty, value); }
}
public static readonly DependencyProperty IsDefaultSortColumnProperty =
DependencyProperty.Register("IsDefaultSortColumn", typeof(bool),
typeof(SortableGridViewColumn), new UIPropertyMetadata(false));
}
public class SortableListView : ListView
{
public SortableListView()
{
// add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView.
this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClickedHandler));
}
SortableGridViewColumn lastSortedOnColumn = null;
ListSortDirection lastDirection = ListSortDirection.Ascending;
public void Sort(string sortBy, ListSortDirection direction)
{
ICollectionView dataView = CollectionViewSource.GetDefaultView
(this.ItemsSource);
//Check if dataView isn't null
if (dataView != null)
{
dataView.SortDescriptions.Clear();
SortDescription sd1 = new SortDescription("#isenabled", direction);
dataView.SortDescriptions.Add(sd1);
SortDescription sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
}
private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
if (headerClicked != null &&
headerClicked.Role != GridViewColumnHeaderRole.Padding)
{
// attempt to cast to the sortableGridViewColumn object.
SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;
// ensure that the column header is the correct type and a sort property has been set.
if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
{
ListSortDirection direction;
// determine if this is a new sort, or a switch in sort direction.
if (lastSortedOnColumn == null
|| String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName)
|| !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
{
direction = ListSortDirection.Descending;
}
else
{
if (lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
}
// get the sort property name from the column's information.
string sortPropertyName = sortableGridViewColumn.SortPropertyName;
// Sort the data.
Sort(sortPropertyName, direction);
lastSortedOnColumn = sortableGridViewColumn;
lastDirection = direction;
}
}
}
}
public class SortList : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//If the value is null tell binding engine to do nothing
if (value == null)
{
return Binding.DoNothing;
}
ListCollectionView view = (ListCollectionView)
CollectionViewSource.GetDefaultView(value);
SortDescription sort_isdisabled =
new SortDescription("#isenabled",
ListSortDirection.Ascending);
view.SortDescriptions.Add(sort_isdisabled);
SortDescription sort_id =
new SortDescription("#id",
ListSortDirection.Ascending);
view.SortDescriptions.Add(sort_id);
return view;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
}
The XAML is as follows:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:.....MiscellaneousCAESettings"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0" >
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<DataTemplate x:Key="textTemplate">
<TextBox HorizontalAlignment= "Stretch"
IsEnabled="{Binding XPath=./#isenabled}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
GotFocus="txtBoxGotFocus"
Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding XPath="./#value" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:TextBoxMinMaxValidation>
<local:TextBoxMinMaxValidation.DataType>
<local:DataTypeCheck
Datatype="{Binding Source={StaticResource dataProvider}, XPath='/[#id=CustomerServiceQueueName]'}"/>
</local:TextBoxMinMaxValidation.DataType>
<local:TextBoxMinMaxValidation.ValidRange>
<local:Int32RangeChecker
Minimum="{Binding Source={StaticResource dataProvider}, XPath=./#min}"
Maximum="{Binding Source={StaticResource dataProvider}, XPath=./#max}"/>
</local:TextBoxMinMaxValidation.ValidRange>
</local:TextBoxMinMaxValidation>
</Binding.ValidationRules>
</Binding >
</TextBox.Text>
</TextBox>
</DataTemplate>
<DataTemplate x:Key="dropDownTemplate">
<ComboBox Name="cmbBox" HorizontalAlignment="Stretch"
SelectedIndex="{Binding XPath=./#value}"
ItemsSource="{Binding XPath=.//OPTION/#value}"
IsEnabled="{Binding XPath=./#isenabled}"
/>
</DataTemplate>
<DataTemplate x:Key="booldropDownTemplate">
<ComboBox Name="cmbBox" HorizontalAlignment="Stretch"
SelectedIndex="{Binding XPath=./#value, Converter={StaticResource boolconvert}}">
<ComboBoxItem>True</ComboBoxItem>
<ComboBoxItem>False</ComboBoxItem>
</ComboBox>
</DataTemplate>
<local:ControlTemplateSelector
x:Key="myControlTemplateSelector"/>
<Style x:Key="StretchedContainerStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template" Value="{DynamicResource ListBoxItemControlTemplate1}"/>
</Style>
<ControlTemplate x:Key="ListBoxItemControlTemplate1" TargetType="{x:Type ListBoxItem}">
<Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" Padding="{TemplateBinding Padding}" BorderThickness="0,0.5,0,0.5">
<GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
<Style x:Key="CustomHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="LightGray" />
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Padding" Value="2,0,2,0"/>
</Style>
</UserControl.Resources>
<Grid x:Name="GridViewControl" Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="34"/>
</Grid.RowDefinitions>
<ListView x:Name="ListViewControl" Grid.Row="0" ItemContainerStyle="{DynamicResource StretchedContainerStyle}"
ItemTemplateSelector="{DynamicResource myControlTemplateSelector}"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource dataProvider},
XPath=//CONFIGURATION}">
<ListView.View >
<GridView >
<GridViewColumn Header="ID" HeaderContainerStyle="{StaticResource CustomHeaderStyle}" DisplayMemberBinding="{Binding XPath=./#id}"/>
<GridViewColumn Header="VALUE" HeaderContainerStyle="{StaticResource CustomHeaderStyle}" CellTemplateSelector="{DynamicResource myControlTemplateSelector}" />
<GridViewColumn Header="DATATYPE" HeaderContainerStyle="{StaticResource CustomHeaderStyle}" DisplayMemberBinding="{Binding XPath=./#data_type}"/>
<GridViewColumn Header="DESCRIPTION" HeaderContainerStyle="{StaticResource CustomHeaderStyle}"
DisplayMemberBinding="{Binding XPath=./#description}"
Width="{Binding ElementName=ListViewControl, Path=ActualWidth}"/>
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Row="1">
<Button Grid.Row="1" HorizontalAlignment="Stretch" Height="34" HorizontalContentAlignment="Stretch" >
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Center" Orientation="Horizontal" FlowDirection="RightToLeft" Height="30">
<Button Grid.Row="1" Content ="Apply" Padding="0,0,0,0 " Margin="6,2,0,2" Name="btn_Apply" HorizontalAlignment="Right" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="132" IsTabStop="True" Click="btn_ApplyClick" Height="24" />
</StackPanel >
</Button>
</StackPanel >
</Grid>

Resources