WPF ListBox, how to hide border and change selected item background color?

To hide the border, use <ListBox BorderThickness=”0″/> If you don’t want to have a selection, use an ItemsControl instead of the ListBox. The following code hides the border around the ListBox and does always show a white background on the item (if its generated through the ItemsSource-property). <ListBox BorderThickness=”0″ HorizontalContentAlignment=”Stretch”> <ListBox.ItemContainerStyle> <Style TargetType=”ListBoxItem”> <Setter Property=”Padding” … Read more

How to customize startup of WPF application?

You can remove the StartupUri attribute from the App.xaml. Then, by creating an override for OnStartup() in the App.xaml.cs, you can create your new instance of your Dispatcher class. Here’s what my quick app.xaml.cs implementation looks like: public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new MyClassIWantToInstantiate(); } } … Read more

WinRT and WPF in Windows 8

WinRT is a replacement for the Winapi. The api is native, very unlike WPF that runs as a layer on top of the CLR. It certainly resembles WPF, part of what causes confusion. It adopted the metadata format of managed code, replacing type libraries of old. And uses XAML for UI designs, much like WPF, … Read more

WPF DataGrid: how do I stop auto scrolling when a cell is clicked?

Define an EventSetter in the DataGrid.RowStyle to call a handler that prevents the row from being brought into view: XAML <DataGrid> <DataGrid.RowStyle> <Style TargetType=”{x:Type DataGridRow}”> <EventSetter Event=”Control.RequestBringIntoView” Handler=”DataGrid_Documents_RequestBringIntoView” /> </Style> </DataGrid.RowStyle> </DataGrid> Handler private void DataGrid_Documents_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) { e.Handled = true; }

How to disable a databound ListBox item based on a property value?

You can use ItemContainerStyle: <ListBox> <ListBox.ItemContainerStyle> <Style TargetType=”{x:Type ListBoxItem}”> <Style.Triggers> <DataTrigger Binding=”{Binding YourPropertyName}” Value=”False”> <Setter Property=”IsEnabled” Value=”False”/> </DataTrigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox>

Window vs User Control

A Window is as the name suggests a window, it can be closed, minimized, resized etc. This should be quite intuitive. A UserControl on the other hand is a composite component/module which can be placed inside other controls and is itself made up of controls (possibly even other UserControls), the main use for UserControls is … Read more