带有超链接的列表框 ->选择已更改

发布于 2024-12-07 09:35:39 字数 1799 浏览 0 评论 0原文

我想用 xaml 绑定实现这样的功能:

列表框包含超链接。

单击超链接时 - 我们转到另一个框架

,但 SelectedItem 也必须更改,并且在另一个框架上我们显示有关所选项目的信息。

我想要它而不订阅点击/选定事件。 我的列表框的唯一声明性

示例

 <ListBox Grid.Row="1" 
        x:Name="OrderTypesListBox"          
        ItemsSource="{Binding OrderTypes, Mode=OneWay}"
        SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"            
    >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                <TextBlock Text="{Binding Name}" />
                    <HyperlinkButton Style="{StaticResource LinkStyle}" NavigateUri="/WindowPage" TargetName="ContentFrame" Content="WindowPage"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>      

现在像这样解决

<ListBox Grid.Row="1" 
        x:Name="OrderTypesListBox"          
        ItemsSource="{Binding OrderTypes, Mode=OneWay}"
        SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"            
    >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <HyperlinkButton                            
                    TargetName="ContentFrame" 
                    NavigateUri="{Binding OrderTypeNextPage}"                           
                    Content="{Binding Name}"
                    Click="HyperlinkButton_Click"
                    Tag="{Binding}"
                />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
    {
        OrderTypesListBox.SelectedItem = (sender as HyperlinkButton).Tag;
    }

I want to do with xaml bindings such feature:

Listbox contains hyperlinks.

When hyperlink clicked - we go to another frame

But also SelectedItem must changed, and on another frame we show info about selected item.

I want it without subscribing click/selected events. Only declarative

Example of my listbox

 <ListBox Grid.Row="1" 
        x:Name="OrderTypesListBox"          
        ItemsSource="{Binding OrderTypes, Mode=OneWay}"
        SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"            
    >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                <TextBlock Text="{Binding Name}" />
                    <HyperlinkButton Style="{StaticResource LinkStyle}" NavigateUri="/WindowPage" TargetName="ContentFrame" Content="WindowPage"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>      

Now solve like that

<ListBox Grid.Row="1" 
        x:Name="OrderTypesListBox"          
        ItemsSource="{Binding OrderTypes, Mode=OneWay}"
        SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"            
    >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <HyperlinkButton                            
                    TargetName="ContentFrame" 
                    NavigateUri="{Binding OrderTypeNextPage}"                           
                    Content="{Binding Name}"
                    Click="HyperlinkButton_Click"
                    Tag="{Binding}"
                />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
    {
        OrderTypesListBox.SelectedItem = (sender as HyperlinkButton).Tag;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2024-12-14 09:35:39

不要使用超链接按钮。当 ViewModel 中的 SelectedItem 发生更改时,执行所需的操作。

编辑:如果您需要响应所有点击事件,即使该项目已被选中,那么您可以使用行为来执行此操作。只需为 TextBlock 创建一个在 TextBlock 单击事件上导航的行为即可。

Edit2: 行为的编码非常简单且易于使用(并且不会破坏 MVVM 范式)。

public class NavigatingTextBlockBehavior : Behavior<TextBlock>
{
  protected override void OnAttached()
  {
    AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseDown);
  }

  private void OnMouseDown(object sender, MouseButtonEventArgs e)
  {
      NavigationService.Navigate(new Uri("/WindowPage"));
  }
}

Don't use a HyperlinkButton. Perform the needed actions when the SelectedItem changes in your ViewModel.

Edit: If you need to respond to all click events even if the item is already selected then you can use a Behavior to do this. Just create a behavior for the TextBlock that navigates on the TextBlock click event.

Edit2: Behaviors are pretty simple to code up and easy to use (and don't break the MVVM paradigm).

public class NavigatingTextBlockBehavior : Behavior<TextBlock>
{
  protected override void OnAttached()
  {
    AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseDown);
  }

  private void OnMouseDown(object sender, MouseButtonEventArgs e)
  {
      NavigationService.Navigate(new Uri("/WindowPage"));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文