【发布时间】:2018-01-04 03:42:12
【问题描述】:
在一个简单的视图中,我想显示两个按钮,它们具有内容图像,它们是通过加载在 App.xaml 中的额外 style.xaml 中的资源提供的。
<BitmapImage x:Key="AddIcon" UriSource="pack://application:,,,/WpfTestBench;component/Images/plus.png"></BitmapImage>
<BitmapImage x:Key="RemoveIcon" UriSource="pack://application:,,,/WpfTestBench;component/Images/minus.png"></BitmapImage>
<Style x:Key="AddButtonWithIconStyle" TargetType="{x:Type Button}">
<Setter Property="Content">
<Setter.Value>
<Image Source="{DynamicResource AddIcon}" Stretch="None"
VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RemoveButtonWithIconStyle" TargetType="{x:Type Button}">
<Setter Property="Content">
<Setter.Value>
<Image Source="{DynamicResource RemoveIcon}" Stretch="None"
VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Setter.Value>
</Setter>
</Style>
在我看来,我使用这样的样式:
<DockPanel Margin="0,0,5,0" DockPanel.Dock="Bottom" LastChildFill="False" >
<Button Command="{Binding DeleteCommand}" DockPanel.Dock="Right"
Style="{StaticResource RemoveButtonWithIconStyle}"/>
<Button Command="{Binding AddCommand}" DockPanel.Dock="Right" Margin="5,0"
Style="{StaticResource AddButtonWithIconStyle}"/>
</DockPanel>
到目前为止,这看起来不错。 但是,当我通过 ContentPresenter 将另一个视图添加到该视图中时,该视图具有基本相同的内容(同样具有上述按钮样式),前两个按钮(父视图)的显示不再起作用。我得到的只是两个带有按钮功能的小圆圈。
<ContentPresenter Content="{Binding SomeEmbeddedViewModel, UpdateSourceTrigger=PropertyChanged}"/>
为什么会这样? ContentPresenter 是否以某种方式阻止共享资源?
【问题讨论】:
标签: c# wpf button styles contentpresenter