我想添加一個按鈕到自定義ListView(MyListView),它觸發MyListView中定義的命令(MyCustomCommand)。我通過應用ControlTemplate添加了按鈕(和標題文本)。問題是,我沒有找到一種方法來觸發MyCustomCommand單擊按鈕時。我最終想要實現的是打開一個Popup或ContextMenu,我可以在ListView中選擇哪些列應該可見。WPF:綁定到來自ControlTemplate的命令
這裏是我的模板來源:
<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>
這裏是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; }
}
這裏是創建MyListView的簡單實例的XAML:
<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>
注意綁定到MyListView中的DependencyProperty的HeaderTitle。這按預期工作。爲什麼它不像命令一樣工作?任何線索如何使這項工作?
謝謝很多。這解決了我的情況:)現在我可以在命令執行時打開一個Popup。 – 2010-10-06 12:26:11
我遇到了一個新問題...觸發命令的按鈕只能在窗口的MyListView的第一個實例中使用(啓用)。它與關鍵字Static有什麼關係:Command = {x:Static local:MyListView。MyCustomCommand} – 2010-10-07 10:56:32
當命令的CanExecute爲false或命令沒有附加Execute處理程序時,帶命令的按鈕被禁用。確保CanExecute中沒有任何奇怪的事情發生,並且CommandBinding正在每個ListView實例上設置,而不是靜態的上下文中,這隻會影響第一個ListView。 – 2010-10-07 11:39:47