当内容不是文本

时间:2015-04-25 20:58:49

标签: wpf xaml contentcontrol contentpresenter

我们假设我的ContentPresenter包含Viewbox内部Path而不是某些文字,如何更改Path s的颜色来自Content Presenter

示例

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

我的ContentPresenter ControlTemplate让我们说Button

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

Icon分配给ContentSource属性只是意味着ContentPresenter将该Viewbox作为内容。

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

希望我很清楚。

更新:

我拼命想要设置ContentPresenter的TextElement.Foreground属性和&#34; Relativesource&#34;将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>
    ...

1 个答案:

答案 0 :(得分:2)

你的第二次尝试应该有效。它之所以没有,是因为你绑定了 Attached Property

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

 int yearofpurchase;
    int monthofpurchase;
    int CurrentMonth;
    int CurrentYearend;
    float Cost;
    float DiffinAccumdepnatbeggining;
    float DepnRate1;
    bool res = int.TryParse(txtYearofPurchased.Text, out yearofpurchase);
    bool MOP = int.TryParse(txtMonthofPurchased.Text, out monthofpurchase);
    bool CM = int.TryParse(txtCurrentMonthEnd.Text, out CurrentMonth);// present month
    bool CYE = int.TryParse(txtCurrentYearEnd.Text, out CurrentYearend);// present year
    bool cost = float.TryParse(txtCost.Text, out Cost);
    bool DCB = float.TryParse(txtDiffinAccumdepnatbeggining.Text, out DiffinAccumdepnatbeggining);
    bool DR = float.TryParse(txtDepnRate1.Text, out DepnRate1);

唯一的变化是Fill="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 字符串周围的括号。有关详细信息,请参阅WPF Attached Property Data Binding