2015-04-25 2 views
0

Допустим, что мой ContentPresenter содержит Viewbox с Path S внутри вместо какой-нибудь текст, как изменить цвет этих Path с от Content PresenterНабор контента цвет ContentPresenter в случае, если содержание не текст

Пример

Я это 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 как содержание.

Что следует поместить в свойство Fill элемента Path и какое свойство следует изменить в ContentPresenter, чтобы его значение распространялось на свойство Fill?

Надеюсь, что я был чист.

Update:

Я отчаянно пытался установить свойство TextElement.Foreground в ContentPresenter и "RelativeSource" привязать свойство Fill The Path к нему, но это предсказуемо не работает.

<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> 
    ... 
+0

Пожалуйста, определите _ "не работает" _. Каковы симптомы? – Gusdor

ответ

2

Ваша вторая попытка должна работали. Причина этого не в том, что вы привязываетесь к Attached Property.

Обязательное связывание свойств требует специального синтаксиса. Окно вывода в visual studio, вероятно, говорит вам, что путь не найден. Это должно работать:

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

Единственное изменение заключается в круглые скобки вокруг Path строки. Для получения дополнительной информации см. WPF Attached Property Data Binding.

+0

Мать скорлупы !! Я проигнорировал все о них! – AymenDaoudi

Смежные вопросы