我是WPF新手。我尝试绑定到listBox元素上的click事件。元素是TextBlock,所以它没有按预期工作,所以我决定创建自定义控件:
ClickableTextBlock:
XAML:
<Button VerticalAlignment="Center" Name="btnButton" HorizontalAlignment="Center" >
<Button.Template>
<ControlTemplate>
<TextBlock Name="tbText"></TextBlock>
</ControlTemplate>
</Button.Template>
</Button>
我想要实现的是能够从主xaml代码控制哪里是listBox,dataContext的哪个属性绑定到自定义控件以及哪个命令绑定到此自定义控件的click事件:< / p>
<ListBox ItemsSource="{Binding Items.Result}" Margin="0,5,0,10" x:Name="listBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" VerticalAlignment="Center">
<!-- my custom control here: -->
<controls:ClickableTextBlock ... <HERE PUT THE CODE...> >
</controls:ClickableTextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果可能的话,最好的方法是什么?