1

Since there is no link button in WPF I created a link button using hyperlink and text block controls.

There are 3 controls:

<TextBlock  Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"  >                
     <Hyperlink Name="hyplnkIsActiveMarkets"  Click="hyplnkIsActive_Click" Foreground="Blue"  >  
         <TextBlock Name="txtblkIsActiveMarkets" Text="Active"  />     
     </Hyperlink>
 </TextBlock>
 <TextBlock  Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left">                
     <Hyperlink Name="hyplnkIsActiveBudgets"  Click="hyplnkIsActive_Click" Foreground="Blue"  >  
         <TextBlock Name="txtblkIsActiveBudgets"  Text="Active"  />     
     </Hyperlink>
</TextBlock>
<TextBlock  Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left">                
     <Hyperlink Name="hyplnkIsActivePrograms"  Click="hyplnkIsActive_Click" Foreground="Blue"  >  
         <TextBlock Name="txtblkIsActivePrograms"  Text="Active"  />     
     </Hyperlink>
</TextBlock>

All the link buttons calls same click method

private void hyplnkIsActive_Click(object sender, RoutedEventArgs e)
{
    Hyperlink objHyperlink = (Hyperlink)sender;
    TextBlock objTextBlock = new TextBlock();

    if (objHyperlink == hyplnkIsActiveMarkets)
    {
        objTextBlock = txtblkIsActiveMarkets;
    }
    else if (objHyperlink == hyplnkIsActiveBudgets)
    {
        objTextBlock = txtblkIsActiveBudgets;
    }
    else if (objHyperlink == hyplnkIsActivePrograms)
    {
        objTextBlock = txtblkIsActivePrograms;
    }

    if (objTextBlock.Text == "Active")
        ChangeHyperLinkStatus(objHyperlink, objTextBlock, Status.Inactive);
    else ChangeHyperLinkStatus(objHyperlink, objTextBlock, Status.Active);
}

In the click method I check for the text block inside the hyper link individually using if condition.

Is there any easier way to do this? That's basically finding control inside a control?

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86

3 Answers3

2

UPDATE: you can not use VisualTreeHelper.GetParent(...) to get the parent of your hyperlink as you have mentioned hyperlink is not visual. corrected the answer.

See code below.

private void hyplnkIsActive_Click(object sender, RoutedEventArgs e)
{
    Hyperlink objHyperlink = (Hyperlink)sender;
    TextBlock objTextBlock  = (TextBlock)LogicalTreeHelper.GetChildren(objHyperlink)[0]; 
   // This will give logical tree first child of objHyperlink


    if (objTextBlock.Text == "Active")
        ChangeHyperLinkStatus(objHyperlink, objTextBlock, Status.Inactive);
    else 
        ChangeHyperLinkStatus(objHyperlink, objTextBlock, Status.Active);
}

See this article about logical tree on MSDN

Maheep
  • 5,539
  • 3
  • 28
  • 47
1

I think you're going into the wrong direction by relaying your execution logic on controls and not on data.

You can, for example, bind a ICommand or RelayCommand to the buttons, or just subscribe different events, or define a custom DataTemplate where on mouse down the clicked control is assignable to some ModelView property.

Doing in way you do, you create tough coupling between UI and your execution logic. In this case easier use WindowsForm then WPF.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

I got it finally . Thanks to Maheep fa his help

 TextBlock objTextBlock = (TextBlock)LogicalTreeHelper.GetChildren(objHyperlink).Cast<System.Windows.Documents.InlineUIContainer>().FirstOrDefault().Child;
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86