Usercontrol中的绑定命令

时间:2012-07-20 12:04:20

标签: wpf binding

我在xaml

中定义了WPF usercontrol
<UserControl x:Class="AIT.Modules.ComponentEditor.Editor.Controls.AITButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="UC">
    <Grid>
        <Button Background="White" BorderBrush="Transparent">
            <StackPanel Orientation="Horizontal" Margin="10">
                <Image Source="{Binding ElementName=UC, Path=Image}"
                       Width="{Binding ElementName=UC, Path=ImageWidth}"
                       Height="{Binding ElementName=UC, Path=ImageHeight}"/>
                <TextBlock Text="{Binding ElementName=UC, Path=Text}"
                           Margin="10,0,0,0"/>
            </StackPanel>
        </Button>
    </Grid>
</UserControl>

相应的DependyProperty位于

后面的usercontrols代码中

在使用控件的窗口中,我需要将viewmodel命令绑定到按钮

 <controls:AITButton Image="..\..\Resources\folder_images.png" Text="Add file" 

    HorizontalAlignment="Left" VerticalAlignment="Top"
                            Margin="10" Command="{x:Static local:ComponentEditorCommands.AddFileCommand}" CommandParameter="{Binding Path=ObjectId}" />

如何在绑定语句中找到buttons命令参数。

1 个答案:

答案 0 :(得分:0)

为什么不将ICommand添加为DependencyProperty?

    <Button Background="White" BorderBrush="Transparent"
            Command="{Binding ElementName=UC, Path=MyCommand}"
            CommandParameter="{Binding ElementName=UC, Path=MyCommandParameter}">
        <StackPanel Orientation="Horizontal" Margin="10">
            <Image Source="{Binding ElementName=UC, Path=Image}"
                   Width="{Binding ElementName=UC, Path=ImageWidth}"
                   Height="{Binding ElementName=UC, Path=ImageHeight}"/>
            <TextBlock Text="{Binding ElementName=UC, Path=Text}"
                       Margin="10,0,0,0"/>
        </StackPanel>
    </Button>