7

嗨,我在“LabelLink”控件的单击事件上绑定方法时遇到问题。我使用伪 LabelLink 控件,我想每个人都知道这个带有文本框和超链接的解决方案。

这是我的代码:

<TextBlock Margin="10,12,10,4">
    <Hyperlink Name="RegLink"
               NavigateUri="http://registracia.azet.sk/"
               Micro:Message.Attach="[Event Click]=[Action OpenDefaultBrowser(NavigateUri)]"
               FontSize="12">Registrácia</Hyperlink>

问题是我只能在框架元素上绑定方法。

我得到这个编译错误:

Cannot attach type "ActionMessage" to type "Hyperlink". Instances of type "ActionMessage" can only be attached to objects of type "FrameworkElement".

我用谷歌搜索,芽没有找到任何合适的解决方案。

感谢您的建议。

我尝试使用 textBlock 或 Label 控件制作一个假的 linkLabel,但它们没有单击事件处理程序。

4

3 回答 3

11

我已经改编了@Rick 对 Caliburn.Micro 的回答

在 TestView.xaml

<Grid>
    <Grid.Resources>
        <Style x:Key="HyperlinkButton" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>

    <Button Style="{StaticResource HyperlinkButton}" Focusable="False"
        cal:Message.Attach="[Action NavigateTo('http://www.stackoverflow.org')]">
        <TextBlock>
            <Hyperlink Focusable="False">www.stackoverflow.com</Hyperlink>
        </TextBlock>
    </Button>
</Grid>

在 TestViewModel.cs

public void NavigateTo(string url)
    {
        Process.Start(new ProcessStartInfo(url));
    }

我在我的应用程序中使用这种方式并且它有效。希望它可以帮助你。

于 2012-11-27T15:34:59.077 回答
5

您可以将您嵌入HyperlinkButton看起来不像按钮的 a 中,以获取在 aClick上发生的事件FrameworkElement

<Grid>
    <Grid.Resources>
        <Style x:Key="HyperlinkButton" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <StackPanel>
        <Button Name="button1" Style="{StaticResource HyperlinkButton}" Click="Button_Click" Focusable="False">
            <TextBlock>
                <Hyperlink NavigateUri="http://www.stackoverflow.com" Focusable="False">
                    StackOverflow
                </Hyperlink>
            </TextBlock>
        </Button>
    </StackPanel>
</Grid>

也许您可以将此技术应用于 Caliburn.Micro。

于 2011-01-22T05:37:45.397 回答
1

我在 vs2017 中做了这个

ViewModel 中的方法

        public void LinkClick(string url)
    {

        if (!string.IsNullOrEmpty(url))
        {
            System.Diagnostics.Process.Start(new ProcessStartInfo(url));
        }
    }

然后如上在顶部的 View XAML 命名空间声明中

xmlns:cal="http://www.caliburnproject.org"

           <Grid>
               <Grid.Resources>
                  <Style x:Key="HyperlinkButton" TargetType="Button">
                       <Setter Property="Template">
                           <Setter.Value>
                               <ControlTemplate TargetType="Button">
                                   <ContentPresenter/>
                               </ControlTemplate>
                           </Setter.Value>
                        </Setter>
                   </Style>
               </Grid.Resources>
                <!-- Collapsed so you can not see it ! -->
                <TextBlock x:Name="LinkTarget" Text="http://www.google.com" Visibility="Collapsed"/>
                <!-- Bound the TAG to the Collapsed TextBlock Text then pass that into the method in VM -->
               <Button Tag="{Binding ElementName=LinkTarget, Path=Text}" Style="{StaticResource HyperlinkButton}" Focusable="False"
                       cal:Message.Attach="[LinkClick($this.Tag)]">
                    <TextBlock>
                        <Hyperlink Focusable="False">Read More ...</Hyperlink>
                    </TextBlock>
               </Button>
           </Grid>

希望这对其他人有帮助!我知道这是一个旧帖子,但是,我登陆了这里!

于 2018-01-21T20:57:41.620 回答