【问题标题】:Binding command in Usercontrol用户控件中的绑定命令
【发布时间】:2012-07-20 12:04:19
【问题描述】:

我在 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代码后面

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

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

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

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    为什么不将 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      相关资源
      最近更新 更多