开发者

WPF link button

开发者 https://www.devze.com 2023-03-02 17:19 出处:网络
I have a button control to which I want add image as well as hyperlink property, i.e it should be an image button with link to other source. I tried

I have a button control to which I want add image as well as hyperlink property, i.e it should be an image button with link to other source. I tried

<Button Click="OnNavigationRequest"  ToolTip="Orkut">
  <Image Source="C:\Documents and Settings\SaurabhS\My Documents\Visual Studio 2008   \Projects\SaurabhSinhaDemos\WPF_Zone\AddressBook\AddressBook\images\orkut.jpeg"/>
  <Hyperlink NavigateUri="http://www.orkut.com">Orkut</Hyperlink>
</Button>

and in code behind:

 AddHandler(Hyperlink.RequestNavigateEvent, 
            new RoutedEventHandler(OnNavigationRequest));

 public void OnNavigationRequest(object sender, RoutedEventArgs e)
      {
          var source = e.OriginalSource as Hyperlink;
          if (source != null)
              Process.Start(source.NavigateUri.ToString());
      }

But got the following error:

开发者_如何转开发

content set more than once...

How should I do it?


In your code, the Button element contains two child elements. The Button element can only take one child element.

Wrap Image and Hyperlink in a StackPanel or some other layout container and the error will go away (see Int3's answer for an example).


Try following

  <Button>
     <StackPanel Orientation="Horizantal">
       <Image Source="path to the image"/>
       <Hyperlink NavigateUri="http://www.orkut.com"/>
     </StackPanel>
  </Button>
0

精彩评论

暂无评论...
验证码 换一张
取 消