首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑HyperLink NavigationURI WPF

编辑HyperLink NavigationURI WPF
EN

Stack Overflow用户
提问于 2018-05-31 10:39:26
回答 1查看 156关注 0票数 0

我是新来的。我有一个具有多个值的ComboBox和一个HyperLink,每当ComboBox值发生变化时,我都想相应地更改HyperLinkNavigateUri

在cs文件中,我有一个字典,其中键与组合项相同,每个键的值是我要根据ComboBox选择导航到的链接。

代码语言:javascript
复制
 LinkQuery["A"] = "https://google.com";
 LinkQuery["B"] = "https://facebook.com";
 LinkQuery["C"] = "https://Youtube.com";

<ComboBox x:Name="box_ComboBox"  Visibility="Visible"  Grid.Column="5" Grid.Row="4" Width="90"
    ItemsSource="{Binding Path=Fields}"
    IsSynchronizedWithCurrentItem="True"
    SelectedValue="{Binding Path=Field}" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" SelectionChanged="component_ComboBox_SelectionChanged"/>

....

<TextBlock x:Name="LinkToQuery" Grid.Row="39" Grid.Column="1" Grid.ColumnSpan="4" Margin="10">           
            <Hyperlink x:Name="URLQuery" RequestNavigate="Hyperlink_RequestNavigate" Foreground="Blue">
                Selected: A
            </Hyperlink>
 </TextBlock>

和cs文件:

代码语言:javascript
复制
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Process.Start(e.Uri.AbsoluteUri);
        e.Handled = true;
    }

private void component_ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        string selectedComponent = box_ComboBox.SelectedItem.ToString();
        LinkToQuery.Text = string.Format("Selected: {0} ", box_ComboBox.SelectedItem.ToString());
        URLQuery.NavigateUri = new System.Uri(LinkQuery[selectedComponent],System.UriKind.Absolute);

    }

当我更改Combo选择时,文本确实会正确更改,但链接不起作用。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 11:14:11

将一个Run元素放入Hyperlink中,并设置此元素的Text属性:

代码语言:javascript
复制
<TextBlock x:Name="LinkToQuery" Grid.Row="39" Grid.Column="1" Grid.ColumnSpan="4" Margin="10">           
    <Hyperlink x:Name="URLQuery" RequestNavigate="Hyperlink_RequestNavigate" Foreground="Blue">
        <Run x:Name="linkText" Text="Selected: A" />
    </Hyperlink>
</TextBlock>
代码语言:javascript
复制
private void component_ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    string selectedComponent = box_ComboBox.SelectedItem.ToString();
    linkText.Text = string.Format("Selected: {0} ", selectedComponent);
    URLQuery.NavigateUri = new System.Uri(LinkQuery[selectedComponent], System.UriKind.Absolute);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50622360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档