I’d like to find out how to bind the opacity of the object that is part of the ControlTemplate to the object that is part of another ControlTemplate. I tried this but it is not doing anything.
Image x:Name="PART_IconHover" Source="{Binding IconHover}" Opacity="{Binding Opacity, ElementName=border, Mode=OneWay}" />
Below is the code of two ControlTemplates:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderBrush="#FF6E6E6E" BorderThickness="0.5" Opacity="0" Background="#00000000">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
I want to b开发者_C百科ind the opacity to the image in the ControlTemplate below
<ControlTemplate x:Key="ThumbnailContainerTemplate" TargetType="{x:Type ContentControl}">
<Border x:Name="PART_Border" BorderThickness="1" BorderBrush="#FFd9d9d9" Opacity="0" />
<Grid Margin="10">
<Image x:Name="PART_IconHover" Source="{Binding IconHover}" Opacity="{Binding Opacity, ElementName=border, Mode=OneWay}" />
</Grid>
Any ideas are highly appreciated. Thank you in advance!
I don't think that you can bind to elements inside templates like that. The binding system isn't able to find them.
If you just need a numeric value somewhere in your xaml that you want everything to use, you can just add one like this:
<sys:Double x:Key="Opacity">.5</sys:Double>
Then just have everything bind to that. You'll need to add the sys namespace
xmlns:sys="clr-namespace:System;assembly=mscorlib"
As mdm20 said, you can't bind to elements inside templates from outside the template since a template is just used to build up a control. For instance, several Buttons could use the Template in your example so which Button would the ContentControl bind to?
I can't see a re-usable solution to this but one thing that comes to mind is to set the Binding in code behind once the Controls have finished loading like this
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Border border = myButton.Template.FindName("border", myButton) as Border;
Image PART_IconHover = contentControl.Template.FindName("PART_IconHover", contentControl) as Image;
Binding opacityBinding = new Binding("Opacity");
opacityBinding.Mode = BindingMode.OneWay;
opacityBinding.Source = border;
PART_IconHover.SetBinding(Image.OpacityProperty, opacityBinding);
}
Update
Two Controls binding to the border in a Button template. The binding is made in the Control_Loaded event handler.
<ContentControl ...
Loaded="Control_Loaded">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Template" Value="{StaticResource contentTemplate}"/>
</Style>
</ContentControl.Style>
</ContentControl>
<ContentControl ...
Loaded="Control_Loaded">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Template" Value="{StaticResource contentTemplate}"/>
</Style>
</ContentControl.Style>
</ContentControl>
private void Control_Loaded(object sender, RoutedEventArgs e)
{
Border border = myButton.Template.FindName("border", myButton) as Border;
Control control = sender as Control;
Image PART_IconHover = control.Template.FindName("PART_IconHover", control) as Image;
Binding opacityBinding = new Binding("Opacity");
opacityBinding.Mode = BindingMode.OneWay;
opacityBinding.Source = border;
PART_IconHover.SetBinding(Image.OpacityProperty, opacityBinding);
}
精彩评论