Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF图像命令绑定_Wpf - Fatal编程技术网

WPF图像命令绑定

WPF图像命令绑定,wpf,Wpf,我正在将一个WPF应用程序放在一起,其中有一个图像控件,我希望将自定义命令对象绑定到视图模型中,该视图模型将在单击图像时执行。我已经从视图模型中公开了命令对象,只需要将其绑定到图像控件 是否可以将此命令对象绑定到图像控件?如果有任何建议,我们将不胜感激。您需要将图像放入按钮中,并将按钮绑定到命令: <Button Command="{Binding MyCommand}"> <Image Source="myImage.png" /> </Button>

我正在将一个WPF应用程序放在一起,其中有一个图像控件,我希望将自定义命令对象绑定到视图模型中,该视图模型将在单击图像时执行。我已经从视图模型中公开了命令对象,只需要将其绑定到图像控件


是否可以将此命令对象绑定到图像控件?如果有任何建议,我们将不胜感激。

您需要将图像放入按钮中,并将按钮绑定到命令:

<Button Command="{Binding MyCommand}">
    <Image Source="myImage.png" />
</Button>

如果您不想使用标准的chrome按钮,只需更改按钮的模板,如下所示:

<ControlTemplate x:Key="tplFlatButton" TargetType="{x:Type Button}">
    <Border Width="{TemplateBinding Width}"
            Height="{TemplateBinding Height}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}">
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          TextElement.Foreground="{TemplateBinding Foreground}"
                          TextElement.FontFamily="{TemplateBinding FontFamily}"
                          TextElement.FontSize="{TemplateBinding FontSize}"
                          TextElement.FontStretch="{TemplateBinding FontStretch}"
                          TextElement.FontWeight="{TemplateBinding FontWeight}"/>
    </Border>
</ControlTemplate>

请注意,您还需要更改其他属性以覆盖默认按钮样式,否则上面的模板将使用默认按钮背景和边框:

<Style x:Key="stlFlatButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{x:Null}" />
    <Setter Property="BorderBrush" Value="{x:Null}" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template" Value="{StaticResource tplFlatButton}" />
</Style>

避免使用按钮而使用超链接可以更简单:

<TextBlock DockPanel.Dock="Top">
   <Hyperlink Command="{Binding SomeCommand}">
      <Image Source="image.png" />
   </Hyperlink>
</TextBlock>

请注意,这将使用默认文本装饰呈现超链接,因此您需要添加一个样式来删除该装饰-将其放入包含元素的资源字典中可以实现以下目的:

<Style x:Key={x:Type Hyperlink}" TargetType="Hyperlink">
   <Setter Property="TextDecorations"
           Value="{x:Null}" />
</Style>

@Robert Rossney答案的简化版本:

<TextBlock>
    <Hyperlink Command="{Binding SomeCommand}" TextDecorations="{x:Null}">
        <Image Source="{StaticResource image.png}" Width="16" />
    </Hyperlink>
</TextBlock>


包含图像的最佳方法是使用
{StaticResource x}
,请参见如果我希望图像带有命令,而不将其包含在另一个控件中,我个人最喜欢使用另一种解决方案

<Image Source="Images/tick.png" Cursor="Hand" Tooltip="Applies filter">
     <Image.InputBindings>
          <MouseBinding Gesture="LeftClick" Command="{Binding ApplyFilter, Mode=OneTime}" />
     </Image.InputBindings>
</Image>


我设置了它的属性
Hand
Tooltip
,以便更清楚地看到它是一个动作,而不是静态图像。

重置按钮的控制模板,并在控制模板中使用图像

 <Button Width="100" Height="100" Command="{Binding SampleCommand}">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image Stretch="Uniform" Source="Images/tick.png"></Image>
                </ControlTemplate>
            </Button.Template>
        </Button>


据我所知,当按下鼠标按钮时,此解决方案已经执行了命令。我希望在释放鼠标按钮时开始执行。就像一个按钮的行为。只是一句话@弗洛里安:谢谢,我没注意到。我通常不会在点击某个东西的时候停下来。不错的方法。我仍然担心我必须实现的按钮式功能,以获得完整的按钮式体验。有些是显而易见的,我可以管理,但有些不是,在某些情况下,它们可能会以低质量的体验告终。因此,对于可维护性和可访问性,我更喜欢使用按钮图像。但同样,您的方法具有功能性和独创性。