0

我在 xaml 中定义了一个 WPF 用户控件

<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}" />

如何在绑定语句中访问按钮命令参数。

4

1 回答 1

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>
于 2012-07-20T12:24:57.090 回答