Próbuję dodać przycisk do niestandardowego ListView (MyListView), który uruchamia polecenie (MyCustomCommand) zdefiniowane w MyListView. Dodałem przycisk (i tekst tytułu), stosując aplikację ControlTemplate. Problem polega na tym, że nie znalazłem sposobu na wywołanie MyCustomCommand po kliknięciu przycisku. W końcu chciałbym otworzyć menu kontekstowe lub menu kontekstowe, w którym mogę wybrać, które kolumny powinny być widoczne w widoku listy.WPF: Bind to command from ControlTemplate
Oto moje źródło szablon:
<Style TargetType="local:MyListView">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyListView">
<Border Name="Border" BorderThickness="1" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid Background="LightSteelBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Margin="3,3,3,3" Text="{TemplateBinding HeaderTitle}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Stretch" FontSize="16" />
<Button Margin="3,3,3,3" Grid.Column="1"
VerticalAlignment="Center" HorizontalAlignment="Right" Height="20"
Command="{TemplateBinding MyCustomCommand}">A button</Button>
</Grid>
<ScrollViewer Grid.Row="1" Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
<ItemsPresenter />
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Oto definicja MyListView:
public class MyListView : ListView
{
public static readonly DependencyProperty MyCustomCommandProperty =
DependencyProperty.Register("MyCustomCommand", typeof(ICommand), typeof(MyListView));
private static RoutedCommand myCustomCommand;
public ICommand MyCustomCommand
{
get
{
if (myCustomCommand == null)
{
myCustomCommand = new RoutedCommand("MyCustomCommand", typeof(MyListView));
var binding = new CommandBinding();
binding.Command = myCustomCommand;
binding.Executed += binding_Executed;
CommandManager.RegisterClassCommandBinding(typeof(MyListView), binding);
}
return myCustomCommand;
}
}
private static void binding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Command Handled!");
}
public static readonly DependencyProperty HeaderTitleProperty =
DependencyProperty.Register("HeaderTitle", typeof(string), typeof(MyListView));
public string HeaderTitle { get; set; }
}
A oto XAML, który tworzy prostą instancję MyListView:
<local:MyListView VerticalAlignment="Top" HeaderTitle="ListView title">
<ListView.View>
<GridView>
<GridViewColumn Width="70" Header="Column 1" />
<GridViewColumn Width="70" Header="Column 2" />
<GridViewColumn Width="70" Header="Column 3" />
</GridView>
</ListView.View>
<ListViewItem>1</ListViewItem>
<ListViewItem>2</ListViewItem>
<ListViewItem>1</ListViewItem>
<ListViewItem>2</ListViewItem>
</local:MyListView>
Uwaga: nagłówekTytuł, który jest powiązany z DependencyProperty w MyListView. Działa to zgodnie z oczekiwaniami. Dlaczego nie działa tak samo z poleceniami? Jakieś wskazówki, jak to zrobić?
Dzięki dużo. To rozwiązało moją sprawę :) Teraz mogę otworzyć wyskakujące okienko po wykonaniu polecenia. –
Wystąpił nowy problem ... Przycisk uruchamiający polecenie jest dostępny tylko (włączony) w pierwszym wystąpieniu MyListView w oknie. Czy ma to coś wspólnego ze słowem kluczowym Static w: Command = {x: Static local: MyListView.MyCustomCommand} –
Przyciski z komendami są wyłączane, gdy polecenie CanExecute ma wartość false lub polecenie nie zawiera dołączonej procedury obsługi Execute. Upewnij się, że nie dzieje się nic dziwnego z CanExecute i że CommandBinding jest konfigurowany na każdej instancji ListView, a nie w statycznym kontekście, który będzie miał wpływ tylko na pierwszy. –