Last active
August 29, 2015 14:02
-
-
Save julesx/13771f146bc97a34bb7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.ObjectModel; | |
using System.Windows; | |
namespace WpfApplication28 | |
{ | |
public partial class MainWindow : Window | |
{ | |
public ObservableCollection<MyItem> MyItems { get; set; } | |
private void MyListBox_OnPreviewMouseUp(object sender, MouseButtonEventArgs e) | |
{ | |
if (e.OriginalSource.GetType() == typeof(ScrollViewer)) | |
return; | |
if (e.ChangedButton == MouseButton.Right && e.ClickCount == 1 && MyListBox.SelectedItems.Count == 1) | |
{ | |
SelectedItem.ShowDetails(null); | |
} | |
if (e.ChangedButton == MouseButton.Right && e.ClickCount == 1 && MyListBox.SelectedItems.Count > 1) | |
{ | |
var cm = new ContextMenu(); | |
var mi = new MenuItem(); | |
mi.Header = "Red"; | |
cm.Items.Add(mi); | |
mi = new MenuItem(); | |
mi.Header = "Blue"; | |
cm.Items.Add(mi); | |
mi = new MenuItem(); | |
mi.Header = "Green"; | |
cm.Items.Add(mi); | |
MyListBox.ContextMenu = cm; | |
MyListBox.ContextMenu.IsOpen = true; | |
} | |
} | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
MyItems = new ObservableCollection<MyItem>(); | |
MyItems.Add(new MyItem("1")); | |
MyItems.Add(new MyItem("2")); | |
MyItems.Add(new MyItem("3")); | |
MyItems.Add(new MyItem("4")); | |
MyItems.Add(new MyItem("5")); | |
MyItems.Add(new MyItem("6")); | |
MyItems.Add(new MyItem("7")); | |
MyItems.Add(new MyItem("8")); | |
DataContext = this; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApplication28.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="MainWindow" Height="350" Width="525"> | |
<Grid> | |
<ListBox PreviewMouseDown="UIElement_OnPreviewMouseDown" SelectedItem="{Binding SelectedItem}" SelectionMode="Extended" ItemsSource="{Binding MyItems}"> | |
<ListBox.ItemTemplate> | |
<DataTemplate> | |
<Border BorderBrush="Gray" BorderThickness="1" Width="100" Height="100" Background="Transparent"> | |
<!--<Border.InputBindings> | |
<MouseBinding MouseAction="RightClick" Command="{Binding CmdShowDetails}" /> | |
</Border.InputBindings>--> | |
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Name}" /> | |
</Border> | |
</DataTemplate> | |
</ListBox.ItemTemplate> | |
</ListBox> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
using System.Windows.Input; | |
namespace WpfApplication28 | |
{ | |
public class MyItem | |
{ | |
public string Name { get; set; } | |
public MyItem(string name) | |
{ | |
Name = name; | |
} | |
private RelayCommand _showDetails; | |
public ICommand CmdShowDetails { get { return _showDetails ?? (_showDetails = new RelayCommand(ShowDetails)); } } | |
public void ShowDetails(object o) | |
{ | |
MessageBox.Show("details for: " + Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment