How to handle the SelectionChanged event of ComboBox with MVVM in wpf?

MVVM solution: Bind the ItemsSource and SelectedItem properties of the ComboBox to properties in your ViewModel: <ComboBox ItemsSource=”{Binding MyItems}” SelectedItem=”{Binding MySelectedItem}”/> In MainViewModel.cs: public ObservableCollection<string> MyItems { get; set; } private string _mySelectedItem; public string MySelectedItem { get { return _mySelectedItem; } set { // Some logic here _mySelectedItem = value; } } Code-behind solution: … Read more

How to get children of a WPF container by type?

This extension method will search recursively for child elements of the desired type: public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject { if (depObj == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var result = (child as T) ?? GetChildOfType<T>(child); if (result … Read more

An integer value in WPF Resources?

To make that declaration you need to import the System namespace: xmlns:sys=”clr-namespace:System;assembly=mscorlib” … <sys:Int32 x:Key=”MyValue”>1234</sys:Int32> Note: you will need to use a Double for most WPF properties instead of an Int32

wpf datagrid alternate row coloring

Finally, this is what I ended up with for generically setting alternate row colors. <Style TargetType=”{x:Type DataGrid}”> <Setter Property=”Background” Value=”#FFF” /> <Setter Property=”AlternationCount” Value=”2″ /> </Style> <Style TargetType=”{x:Type DataGridRow}”> <Style.Triggers> <Trigger Property=”ItemsControl.AlternationIndex” Value=”0″> <Setter Property=”Background” Value=”#CCC”></Setter> </Trigger> <Trigger Property=”ItemsControl.AlternationIndex” Value=”1″> <Setter Property=”Background” Value=”#EEE”></Setter> </Trigger> </Style.Triggers> </Style>

WPF ComboBox performance problems by binding a large collections

According to this blog: http://vbcity.com/blogs/xtab/archive/2009/12/15/wpf-using-a-virtualizingstackpanel-to-improve-combobox-performance.aspx I’ve tested it with this code: <ComboBox Name=”cbBlah” ItemsSource=”{Binding}”> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ComboBox.ItemsPanel> </ComboBox> It works fine for first time and next times. It’s not necessary to code these lines: <VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing=”True” VirtualizingStackPanel.VirtualizationMode=”Recycling” />

In WPF, how can I determine whether a control is visible to the user?

You can use this little helper function I just wrote that will check if an element is visible for the user, in a given container. The function returns true if the element is partly visible. If you want to check if it’s fully visible, replace the last line by rect.Contains(bounds). private bool IsUserVisible(FrameworkElement element, FrameworkElement … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)