在WPF中查找另一个控件内的控件

问题描述:

由于WPF中没有链接按钮,我使用超链接和文本块控件创建了一个链接按钮。在WPF中查找另一个控件内的控件

有3个控件:

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

所有链接按钮调用相同的点击方法

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

在点击方法我的文本块检查里面的超级链接单独使用,如果条件。

有没有更简单的方法来做到这一点?这基本上是在控制内部找到控制权?

+0

你可以使用一个for循环,并检查控制 – MethodMan 2011-12-30 06:43:06

+0

这可以帮助你:[WPF的方式找到能够控制@ SO(http://*.com/questions/636383/wpf -ways找到的 - 控制)。 – 2011-12-30 06:43:44

+0

foreach(parent.Controls中的控件控件){}在括号内做你的类型检查和或铸造 – MethodMan 2011-12-30 06:48:53

我终于明白了。由于Maheep发他的帮助

TextBlock objTextBlock = (TextBlock)LogicalTreeHelper.GetChildren(objHyperlink).Cast<System.Windows.Documents.InlineUIContainer>().FirstOrDefault().Child; 

UPDATE:不能使用VisualTreeHelper.GetParent(...),让您的超链接的父母为你提到的超链接是不可视。纠正了答案。

查看下面的代码。

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

请参阅本文约logical tree on MSDN

+0

为什么要投票...? – Maheep 2011-12-30 06:47:00

+0

我收到异常“'System.Windows.Documents.Hyperlink'不是Visual或Visual3D。” – 2011-12-30 06:49:54

+0

更新了答案 – Maheep 2011-12-30 06:56:58

我觉得你在controlsdata进入了错误的方向中继的执行逻辑。

可以,例如,绑定ICommandRelayCommand的按钮,或者只是订阅不同的事件,或自定义一个DataTemplate其中上按下鼠标点击的控制是分配给一些ModelView财产。

这样做,你创建UI和你的执行逻辑之间的难以耦合。 在这种情况下,使用WindowsForm然后WPF更容易。