Slider On/Off Switch in WPF

I haven’t seen a tutorial on this exact problem, but i guess you can start by launching Expression Blend and putting a CheckBox on it. Then select the CheckBox, go to main menu – Object -> Edit Style -> Edit a Copy This will make Blend generate the default style of CheckBox so you’re able … Read more

Make WPF textbox as cut, copy and paste restricted

Cut, Copy, and Paste are the common commands used any application. <TextBox CommandManager.PreviewExecuted=”textBox_PreviewExecuted” ContextMenu=”{x:Null}” /> In the above textbox code we can restrict these commands in PreviewExecuted event of CommandManager Class. In the code behind add the code below and your job is done. private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e) { if (e.Command == ApplicationCommands.Copy … Read more

WPF ListBoxItem double-click?

It is possible to bind commands with parameters to ListBoxItems without using code-behind or attached behaviors, simply by using InputBindings with a MouseBinding, as shown before in this answer. Example ListBox with MouseBinding for LeftDoubleClick: <ListBox ItemsSource=”{Binding MyDataSource}”> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text=”{Binding MySourceItemName}”> <TextBlock.InputBindings> <MouseBinding MouseAction=”LeftDoubleClick” Command=”{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}” CommandParameter=”{Binding MySourceItemId}” /> </TextBlock.InputBindings> … Read more

WPF – choose startup window based on some condition

look into App.xaml remove StartupUri=”MainWindow.xaml” add Startup=”Application_Startup” new event Handler <Application x:Class=”YourProject.App” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Startup=”Application_Startup”> form code behind App.xaml.cs create Application_Startup like… private void Application_Startup(object sender, StartupEventArgs e) { //add some bootstrap or startup logic var identity = AuthService.Login(); if (identity == null) { LoginWindow login = new LoginWindow(); login.Show(); } else { MainWindow mainView … Read more

Gaps between items in my ListBox

This is because of the padding inside the default ItemContainerStyle for ListBoxItem. To remove this you can override the ItemContainerStyle. For example just try the below Empty ItemContainerStyle to your ListBox and you can see the margin is no more. <ListBox > <ListBox.ItemsPanel> <ItemsPanelTemplate > <VirtualizingStackPanel IsItemsHost=”True” Orientation=”Horizontal”/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemContainerStyle> <Style TargetType=”{x:Type ListBoxItem}”> <Setter … Read more

Stop WPF ScrollViewer automatically scrolling to perceived content

By default when a control receives the logical focus, FrameworkElement calls its own BringIntoView method (from within its OnGotFocus method if it has keyboard focus). That results in a RequestBringIntoView event being generated that bubbles up the element tree to allow ancestor elements to bring that portion of the element into view. The ScrollViewer listens … Read more

Get the item doubleclick event of listview

<ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <EventSetter Event=”MouseDoubleClick” Handler=”listViewItem_MouseDoubleClick” /> </Style> </ListView.ItemContainerStyle> The only difficulty then is if you are interested in the underlying object the listviewitem maps to e.g. private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListViewItem item = sender as ListViewItem; object obj = item.Content; }