我想了解如何将作为ControlTemplate一部分的对象的不透明度绑定到另一个ControlTemplate的对象。 我尝试了这个,但它没有做任何事情。
Image x:Name="PART_IconHover" Source="{Binding IconHover}" Opacity="{Binding Opacity, ElementName=border, Mode=OneWay}" />
以下是两个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>
我想将不透明度绑定到下面的ControlTemplate中的图像
<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>
任何想法都受到高度赞赏。提前谢谢!
答案 0 :(得分:2)
我认为你不能像这样绑定到模板中的元素。绑定系统无法找到它们。
如果您只需要在xaml中的某个位置使用数字值,那么您可以添加如下所示:
<sys:Double x:Key="Opacity">.5</sys:Double>
然后让一切都绑定到那个。您需要添加sys命名空间
xmlns:sys="clr-namespace:System;assembly=mscorlib"
答案 1 :(得分:1)
正如mdm20所说,你不能从模板外部绑定模板内的元素,因为模板只是用来构建一个控件。例如,几个按钮可以在您的示例中使用模板,以便ContentControl将哪个Button绑定到?
我无法看到一个可重复使用的解决方案,但有一点我想到的是,一旦控件完成加载后,在代码中设置Binding
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);
}
<强>更新强>
两个控件绑定到Button模板中的边框。绑定是在Control_Loaded事件处理程序中完成的。
<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);
}