wpf usercontrol,将按钮的命令参数绑定到父usercontrol

时间:2009-08-21 10:25:52

标签: wpf binding user-controls

我有一个包含删除按钮的WPF UserControl,我想将整个UserControl作为CommandParameter传递。

目前绑定设置为CommandParameter =“{Binding RelativeSource = {RelativeSource Self}}”,它给出了按钮,但是如何获得整个控件?

有人可以帮忙吗?

干杯,

安迪

<UserControl x:Class="GTS.GRS.N3.Controls.LabelledTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="155" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Label Name="label" HorizontalAlignment="Left" Width="96">Label</Label>
    <TextBox Name="textBox" Grid.Column="1" />
    <Button Grid.Column="2" Style="{DynamicResource CloseButton}" Command="{Binding RemoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" Visibility="{Binding RemoveVisible}"></Button>
</Grid>
</UserControl>

1 个答案:

答案 0 :(得分:6)

破解了......

<Button 
    Grid.Column="2" 
    Style="{DynamicResource CloseButton}"
    Command="{Binding RemoveCommand}"
    CommandParameter="{Binding RelativeSource=
        {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
    Visibility="{Binding RemoveVisible}" 
    />