, - - . Page UserControl s; x:Class. , Styles.xml :
<ResourceDictionary
x:Class="WPF.Styles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<DataTemplate x:Key="ContactItemTemplate" DataType="local:Person">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Foreground="Black"
Background="Yellow"
Padding="5,10"
Margin="4,3"
Text="{Binding Name}"
>
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit!" Click="btn_EditContact_Click"/>
<MenuItem Header="Delete!" Click="btn_DeleteContact_Click"/>
<MenuItem Header="View!" Click="btn_EditContact_Click"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<Button
Grid.Column="1"
Content="Edit"
Click="btn_EditContact_Click"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
... .cs( partial):
public sealed partial class Styles
{
private void btn_EditContact_Click(object sender, EventArgs args)
{
Debug.WriteLine(args);
}
private void btn_DeleteContact_Click(object sender, EventArgs args)
{
Debug.WriteLine(args);
}
}
- :
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
...
<ItemsControl
ItemsSource="{Binding People}"
Grid.Row="3"
ItemTemplate="{StaticResource ContactItemTemplate}" />
:
public class Person
{
public string Name { get; }
public Person(string name)
{
Name = name;
}
}
... ItemsSource:
public Person[] People { get; } =
{
new Person("Donald Duck"),
new Person("Mickey Mouse"),
new Person("Darth Vader"),
};
- , , btn_EdxtContact_Click. , TextBox;
.
: On ContextMenus
: ContextMenu, .
... TextBox ContextMenu , .