我是WPF新手,我无法弄清楚如何在鼠标悬停时更改ContentControl
控件的子Button
的属性。我的代码看起来像这样:
<Button x:Name="btnAddItem" Height="25" Width="25" Margin="5,0,0,0"
Style="{DynamicResource btnStyle}" ToolTip="Add Item">
<ContentControl Content="ContentControl" Height="20" Width="20"
Template="{DynamicResource contentTemplate}" />
</Button>
现在,在MouseOver
的{{1}}事件中,我想更改Button
的大小以及子Button
的大小。 ContentControl
实际上包含ContentControl
的矢量图像。请帮忙。
答案 0 :(得分:1)
您的Button
会自动拉伸以适应其内容的大小,因此请删除它的Height
和Width
属性。如果要维护Button边缘和ContentControl之间的空间,请使用ContentControl的Margin
属性。
然后,使用ContentControl的DataTrigger
中的Style
更改鼠标悬停在其上的Height
/ Width
。请确保在您的样式中设置Height
/ Width
而不是<ContentControl>
标记,因为如果您在标记中设置它,它将优先于触发值,因此永远不会更改。
<Style x:Key="MyContentControlStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Height" Value="20" />
<Setter Property="Width" Value="20" />
<Setter Property="Margin" Value="5" />
<Setter Property="Content" Value="ContentControl" />
<Setter Property="Template" Value="{DynamicResource contentTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=btnAddItem, Path=IsMouseOver}" Value="True">
<Setter Property="Height" Value="20" />
<Setter Property="Width" Value="20" />
</DataTrigger >
</Style.Triggers>
</Style>
<Button x:Name="btnAddItem" Height="25" Width="25" Margin="5,0,0,0"
Style="{DynamicResource btnStyle}" ToolTip="Add Item">
<ContentControl Style="{StaticResource MyContentControlStyle}" />
</Button>
答案 1 :(得分:0)
为了实现我想要的目标,我使用了Rachel的建议以及Samuel Slade的建议。我做到了这样的事情:
<Button x:Name="btnEditItem" Style="{DynamicResource btnStyle}" Margin="5,0,0,0" ToolTip="Edit Item" Click="btnEditItem_Click">
<ContentControl x:Uid="ContentControl_5" Content="ContentControl" Template=" {DynamicResource contentTemplate}" Margin="2.5"/>
</Button>
我通过BtnStyle通过Setter属性设置按钮的高度和宽度,并通过触发器更改按钮的高度和宽度。
这让我工作得很完美。我感谢您的所有帮助建议。我不确定我是否能够达成这个结论,因为我正在思考一个不同的儿童控制属性路径。再次感谢。