window 界面很多自定义控件方便很多地方用到的控件都是重复的
可以用资源字典 来统一管理 方便修改
自定义一个imagebutton
<Style TargetType="Button" x:Key="ButtonImage"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid Background="{TemplateBinding Background}"> <ContentPresenter></ContentPresenter> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
xaml页面调用 只用在<page.Resources></page.Resources>的中间 添加资源字典文件夹路径就可以了
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source文件夹路径"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
前台添加控件时<Button x:Name="buttonPre" Style="{StaticResource 自定义的控件X:key}" Grid.Row="1" Margin="6" Click="buttonPre_Click">
<Image Source图片位置"/>
</Button>
后台添加
button.Style = Resources["自定义的控件X:key"] as Style;
button.SetResourceReference(Button.StyleProperty, "自定义的控件X:key");