c# - 编辑超链接 NavigationURI WPF
问题描述
我是 WPF 的新手。我有一个ComboBox
具有多个值的 a HyperLink
,每当ComboBox
值发生变化时,我都想相应地更改NavigateUri
。HyperLink
在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);
}
当我更改组合选择时,文本确实会正确更改,但链接不起作用。
谢谢你。
解决方案
将一个Run
元素放在里面Hyperlink
并设置这个元素的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);
}
推荐阅读
- f# - 如何在 f# 中使用 suave 从 http 重定向到 https
- android - DropDownTextView Android 改变颜色
- ios - 在 Xcode 中构建反应本机项目的问题 - 错误:找不到自动链接库 + 未定义符号等
- javascript - 是否有可能用柏树验证阻塞字段?
- azure-sql-database - 使用 Key Vault 访问数据工厂中的 SQL 数据库托管标识
- c - 分段错误 - 分而治之
- tabulator - 在不过滤的情况下隐藏一行?
- javascript - 为什么在html中放置链接css,scss时给我错误
- python - 如何读取文件并将数据转换为数字?
- azure-functions - 使用 Ms graph 设置更改通知(订阅)服务后,如何从 Azure 函数应用程序在 Visual Studio 中获取通知