【问题标题】:Set the ContentPresenter's content color, when the content is not a text设置 ContentPresenter 的内容颜色,当内容不是文本时
【发布时间】:2015-07-04 10:12:28
【问题描述】:

假设我的ContentPresenter 包含一个Viewbox,其中包含Paths 而不是一些文本,我如何从Content Presenter 更改那些Paths 的颜色

示例

我有这个ResourceDictionary

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <Viewbox>
              <Grid>
                <Grid Name="backgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
                <Path Data="M19.833,0L32.5,0 32.5,19.833999 52.334,19.833999 52.334,32.500999 32.5,32.500999 32.5,52.333 19.833,52.333 19.833,32.500999 0,32.500999 0,19.833999 19.833,19.833999z" 
Stretch="Uniform" 
Fill="?????" 
Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                  <Path.RenderTransform>
                    <TransformGroup>
                      <TransformGroup.Children>
                        <RotateTransform Angle="0" />
                        <ScaleTransform ScaleX="1" ScaleY="1" />
                      </TransformGroup.Children>
                    </TransformGroup>
                  </Path.RenderTransform>
                </Path>
              </Grid>
            </Viewbox>
        </ResourceDictionary>

我在ControlTemplate 中有ContentPresenter,比如说Button

<ControlTemplate TargetType="{x:Type local:IconButton}">
       <Border>
          <Grid>                            
              <ContentPresenter x:Name="ContentPresenterIcon" ContentSource="Icon"/>                        
          </Grid>
       </Border>
...

Icon 分配给ContentSource 属性仅意味着ContentPresenter 将该视图框作为内容。

我应该在 Path Element 的 Fill 属性中添加什么,我应该在 ContentPresenter 中更改哪个属性以使其值传播到 Fill 属性?

希望我是清楚的。

更新:

我拼命尝试设置 ContentPresenter 的 TextElement.Foreground 属性并将“Relativesource”绑定到它的 Path 的 Fill 属性,但可以预见的是这不起作用。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
                <Viewbox>
                  <Grid>
                    <Grid Name="backgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
                    <Path Data="M19.833,0L32.5,0 32.5,19.833999 52.334,19.833999 52.334,32.500999 32.5,32.500999 32.5,52.333 19.833,52.333 19.833,32.500999 0,32.500999 0,19.833999 19.833,19.833999z" 
    Stretch="Uniform" 
    Fill="{Binding Path=TextElement.Foreground, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
    Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                      <Path.RenderTransform>
                        <TransformGroup>
                          <TransformGroup.Children>
                            <RotateTransform Angle="0" />
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                          </TransformGroup.Children>
                        </TransformGroup>
                      </Path.RenderTransform>
                    </Path>
                  </Grid>
                </Viewbox>

<ControlTemplate TargetType="{x:Type local:IconButton}">
           <Border>
              <Grid>                            
                  <ContentPresenter x:Name="ContentPresenterIcon" TextElement.Foreground="Red" ContentSource="Icon"/>                        
              </Grid>
           </Border>
    ...

【问题讨论】:

  • 请定义“不起作用”。有什么症状?

标签: wpf xaml contentcontrol contentpresenter


【解决方案1】:

您的第二次尝试应该成功了。没有这样做的原因是您绑定到 Attached Property

附加属性绑定需要特殊语法。 Visual Studio 中的输出窗口可能会告诉您找不到路径。这应该有效:

Fill="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 

唯一的变化是Path 字符串周围的括号。如需更多信息,请参阅WPF Attached Property Data Binding

【讨论】:

  • 括号之母!!我忽略了他们的一切!
猜你喜欢
  • 2016-12-01
  • 2019-03-05
  • 2015-02-12
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2016-05-10
相关资源
最近更新 更多