Snap the current foreground to a Rectangle.Fill object

I use the Mahapps Metro theme in my project. I want to create TabControlwhere the TabItemimage will be. The Mahapps theme provides TabItem text color change when selected TabItem, etc. I want to bind this foreground color to my image. I have a solution to this problem, but I think this is not true.

Bad code (but it works):

<TabItem>
   <TabItem.HeaderTemplate>
      <DataTemplate>
         <Grid Margin="0,5,0,0">
            <TextBlock x:Name="myTextBlock" />
            <Rectangle Width="28.947" Height="25" Fill="{Binding ElementName=myTextBlock, Path=Foreground}">
               <Rectangle.OpacityMask>
                  <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_image_gallery}" />
               </Rectangle.OpacityMask>
            </Rectangle>
         </Grid>
      </DataTemplate>
   </TabItem.HeaderTemplate>
</TabItem>
+4
source share
1 answer

You can use TemplateBindingit if you do not want to mess with the inside TextBlock.

<TabItem.HeaderTemplate>
    <DataTemplate>
        <Grid Margin="0 5 0 0">
            <TextBlock Text="{Binding}" />
            <Rectangle Width="28.947"
                        Height="25"
                        Fill="{TemplateBinding TextElement.Foreground}">
                <Rectangle.OpacityMask>
                    <VisualBrush Stretch="Fill" Visual="{DynamicResource appbar_image_gallery}" />
                </Rectangle.OpacityMask>
            </Rectangle>
        </Grid>
    </DataTemplate>
</TabItem.HeaderTemplate>
+2
source

Source: https://habr.com/ru/post/1611658/


All Articles