2009-08-21 186 views
10

我有一個WPF UserControl,它包含一個刪除按鈕,我想將整個UserControl作爲CommandParameter傳遞。wpf usercontrol,將按鈕的命令參數綁定到父usercontrol

目前綁定設置爲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> 

回答

6

破解了......

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