【问题标题】:Edit HyperLink NavigationURI WPF编辑超链接 NavigationURI WPF
【发布时间】:2018-05-31 10:39:26
【问题描述】:

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

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

 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文件:

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 选择时,文本确实会正确更改,但链接不起作用。

谢谢。

【问题讨论】:

  • LinkQuery 是 Dictionary,Fields 是 CollectionView。

标签: c# wpf hyperlink navigateuri


【解决方案1】:

Hyperlink 中放置一个Run 元素并设置该元素的Text 属性:

<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>

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);
}

【讨论】:

  • 效果很好!太感谢了。能否请您简要解释一下 Run 元素以及它所做的更改?
  • 当您设置 LinkToQuery 的 Text 属性时,您正在清除超链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多