自己现在想修改一人控件模版的外观,就是想根据不同的情况来修改,很自然的要用到绑定,可是绑定只能是依赖属性啊,我就写了一个依赖属性,可是后来发现怎么用,怎么改依赖属性的值都不会传过去,传过到模版 上的都是依赖属性的初始值,后来我又想动太的改变依赖属性的初始值,可是后来发现自己水平实现不了,
谁要是有办法给我说一下啊,
后来我的解决办法就是不去动态的修改控件模版了而是要用一个静态的样式来调用把控件模版包括起来,这样的话就可以直接修改外观的样式,这样就可以用普通属性来进行绑定了。、
以下是源码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:MysherOCRReader"
xmlns:c="clr-namespace:MysherOCRReader.doubleClass">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MoveThumb.xaml"/>
<ResourceDictionary Source="ResizeDecorator.xaml"/>
<ResourceDictionary Source="RotateDecorator.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- ContentControl style to move, resize and rotate items -->
<Style x:Key="DesignerItemStyle" TargetType="ContentControl">
<Style.Resources>
<c:ChangeRegionColor x:Key="change_background"/>
<c:GetPhotoInfoRegionCollection x:Key="REgionCollection"/>
<s:RegionInfo x:Key="RegionCollection"></s:RegionInfo>
</Style.Resources>
<Setter Property="MinHeight" Value="10"/>
<Setter Property="MinWidth" Value="10"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl" >
<Border DataContext="{StaticResource RegionCollection}" >
<Grid x:Name="grd1" Opacity="0.4" Background="{Binding OCRType,Converter={StaticResource change_background},UpdateSourceTrigger=PropertyChanged}">
<Label x:Name="lab_flag" Content="Mysher编号" Height="250" Width="250" Foreground="Black"></Label>
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" >
<Control Name="RotateDecorator"
Template="{StaticResource RotateDecoratorTemplate}"
Visibility="Collapsed"/>
<s:MoveThumb x:Name="MoveThumbDecorator" Template="{StaticResource MoveThumbTemplate}"
Cursor="Hand" />
<Control x:Name="ResizeDecorator"
Template="{StaticResource ResizeDecoratorTemplate}"
Visibility="Collapsed"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
</Grid>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter TargetName="ResizeDecorator" Property="Visibility" Value="Visible"/>
<Setter TargetName="RotateDecorator" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="MoveThumbDecorator" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>