WPF:在某个控件的触发器中链接另一个控件的属性 [英] WPF : Chainging another control's property in some control's Trigger
问题描述
这是我的文本块。
< Image x:Name:imgAnother />
< TextBlock>
这是我的文本块
< TextBlock.Style>
< Style TargetType = TextBlock>
< Setter Property = TextDecorations Value = None />
< Style.Triggers>
< Trigger Property = TextBlock.IsMouseOver Value = True>
< Setter Property = Foreground Value = RoyalBlue />
<!-我想在这里插入代码来更改另一个控件的属性...-->
< / Trigger>
< Trigger Property = TextBlock.IsMouseOver Value = False>
< Setter Property = Foreground Value =#FF808080 />
<!-..这行也是.-->
< / Trigger>
< /Style.Triggers>
< / Style>
< /TextBlock.Style>
< / TextBlock>
我喜欢制作一个xaml代码,该代码可以更改另一个控件的属性,例如 imgAnother。 p>
我该怎么做?
您必须汇总源代码并
您可以创建一个既包含超链接/文本块又包含图像的自定义控件。如果您在此示例中有多个行为块,那么这是首选方法。
如果您不喜欢这样。您可以按照以下方式创建临时匿名控件:
< ControlTemplate x:Key = myCtl TargetType = ContentControl >
< StackPanel>
<图片x:Name = img />
< ContentPresenter x:Name = ctr />
< / StackPanel>
< ControlTemplate.Triggers>
< Trigger SourceName = ctr Property = IsMouseOver Value = True>
< Setter TargetName = ctr Property = Foreground Value = RoyalBlue />
< ;!-我想在此处插入代码来更改另一个控件的属性...-->
< / Trigger>
< Trigger SourceName = ctr Property = IsMouseOver Value = False>
< Setter TargetName = ctr Property = Foreground Value =#FF808080 />
<!-..这行也是.-->
< / Trigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
以上xaml将驻留在Window的资源中。
注意:比起功能齐全的代码片段,这更像是要遵循的轨道!
在体内,您可以通过以下方式引用控件:
< ContentControl模板= {StaticResource myCtl} Content =这是我的文字块 />
希望有帮助。
Here's my textblock.
<Image x:Name:imgAnother/>
<TextBlock>
this is my text block
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="TextDecorations" Value="None"/>
<Style.Triggers>
<Trigger Property="TextBlock.IsMouseOver" Value="True">
<Setter Property="Foreground" Value="RoyalBlue"/>
<!--I like to insert a code at here that changes another control's property...-->
</Trigger>
<Trigger Property="TextBlock.IsMouseOver" Value="False">
<Setter Property="Foreground" Value="#FF808080"/>
<!--..and this line too.-->
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
I like to make a xaml code which can change another control's proerpty, like "imgAnother".
how can i do that?
You must aggregate the source and the target in some way.
You may create either a custom control containing both the hyperlink/textblock and the image. That's the preferred way if you have several blocks that behaves in such example.
If you don't like this. You may create a "temporary" anonymous control as follows:
<ControlTemplate x:Key="myCtl" TargetType="ContentControl">
<StackPanel>
<Image x:Name="img"/>
<ContentPresenter x:Name="ctr" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger SourceName="ctr" Property="IsMouseOver" Value="True">
<Setter TargetName="ctr" Property="Foreground" Value="RoyalBlue"/>
<!--I like to insert a code at here that changes another control's property...-->
</Trigger>
<Trigger SourceName="ctr" Property="IsMouseOver" Value="False">
<Setter TargetName="ctr" Property="Foreground" Value="#FF808080"/>
<!--..and this line too.-->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
The above xaml will reside in your Window's resources.
NOTE: it's much like a track to follow, than a fully functional snippet!
In the body, you may refer the control in such manner:
<ContentControl Template="{StaticResource myCtl}" Content="this is my text block" />
Hope it helps.
这篇关于WPF:在某个控件的触发器中链接另一个控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!