silverlight 控件样式动态绑定
<telerik:RadDiagram x:Name="diagram1" GraphSource="{Binding GraphSource, Mode=TwoWay}"
ShapeTemplate="{StaticResource ShapeTemplate}" SelectionMode="Extended" />
控件样式绑定资源文件(核心关键词StaticResource),所以不能重复Graph,当我重新绑定数据时,只能获取到数据,但是控件样式不发生变化
解决方案:
删除之前的控件,重新new一个,然后从资源文件中获取 模板列赋值
gdDiagram.Children.Clear();
// 新建一个RadDiagram替换之前的
Telerik.Windows.Controls.RadDiagram diagram = new Telerik.Windows.Controls.RadDiagram();
diagram.SelectionMode = (Telerik.Windows.Diagrams.Core.SelectionMode)SelectionMode.Extended;
diagram.GraphSource = viewModel.GraphSource;
diagram.ShapeTemplate = this.Resources["ShapeTemplate"] as DataTemplate;
gdDiagram.Children.Add(diagram);
遇到的问题:(在前台)
1. 添加silverlight资源字典:ResourcesSL.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:scr="clr-namespace:CRS.Common"
xmlns:viewModel="clr-namespace:CRS.OrgChart.VM"
xmlns:tools="clr-namespace:Telerik.Windows.Diagrams.Core;assembly=Telerik.Windows.Diagrams.Core"
xmlns:primitives="clr-namespace:Telerik.Windows.Controls.Diagrams.Primitives;assembly=Telerik.Windows.Controls.Diagrams"
mc:Ignorable="d"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:sp="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" >
<Style TargetType="telerik:RadDiagram">
<Setter Property="AllowCopy" Value="False" />
<Setter Property="AllowCut" Value="False" />
<Setter Property="AllowDelete" Value="False" />
<Setter Property="AllowPaste" Value="False" />
<Setter Property="IsConnectorsManipulationEnabled" Value="False" />
<Setter Property="IsEditable" Value="False" />
<Setter Property="IsResizingEnabled" Value="False" />
<Setter Property="IsRotationEnabled" Value="False" />
<Setter Property="IsSnapEnabled" Value="False" />
<Setter Property="ActiveTool" Value="PointerTool" />
</Style>
<viewModel:ViewModel x:Key="ViewModel" />
<viewModel:BackgroundSelector x:Key="BackgroundSelector">
<viewModel:BackgroundSelector.TopBrush>
<SolidColorBrush Color="#FF9F9E9E" />
</viewModel:BackgroundSelector.TopBrush>
<viewModel:BackgroundSelector.TopChild1Brush>
<SolidColorBrush Color="#FFF7C300" />
</viewModel:BackgroundSelector.TopChild1Brush>
<viewModel:BackgroundSelector.TopChild3Brush>
<SolidColorBrush Color="#FF15ACA9" />
</viewModel:BackgroundSelector.TopChild3Brush>
<viewModel:BackgroundSelector.TopChild2Brush>
<SolidColorBrush Color="#FFE33A33" />
</viewModel:BackgroundSelector.TopChild2Brush>
</viewModel:BackgroundSelector>
<telerik:BooleanToVisibilityConverter x:Key="Converter" />
<Storyboard x:Key="Collapse">
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="" BeginTime="0:0:0" Duration="0:0:0.5" />
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0.5">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Show">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="" BeginTime="0:0:0" Duration="0:0:0.5" />
</Storyboard>
<Style TargetType="telerik:RadDiagramShape">
<Setter Property="Position" Value="{Binding Position, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="Padding" Value="" />
</Style>
<Style TargetType="telerik:RadDiagramConnection">
<Setter Property="Visibility" Value="{Binding Visibility, Mode=TwoWay}" />
<Setter Property="SourceConnectorPosition" Value="Bottom" />
<Setter Property="TargetConnectorPosition" Value="Top" />
<Setter Property="BorderBrush" Value="#FF9F9E9E" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBlock" x:Key="TextBlockNameStyle">
<Setter Property="FontFamily" Value="Segoe UI Light" />
<Setter Property="FontSize" Value="" />
<Setter Property="Foreground" Value="#FFFFFFFF" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="HorizontalAlignment" Value="Left" />
</Style>
<Style TargetType="TextBlock" x:Key="TextBlockStyle">
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="" />
<Setter Property="Foreground" Value="#FF333333" />
</Style>
<Style TargetType="TextBlock" x:Key="TextBlockPositionStyle" BasedOn="{StaticResource TextBlockStyle}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="0 5 0 0" />
</Style>
<Style TargetType="telerik:RadToggleButton" x:Key="RadToggleButtonStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="telerik:RadToggleButton">
<Grid Background="Transparent" Cursor="Hand">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="path1"
Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="#FF333333" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="path2"
Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="#FF333333" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed" />
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="arrow"
Storyboard.TargetProperty="(FrameworkElement.RenderTransform).Angle"
To="" Duration="0:0:0.2" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="arrow"
Storyboard.TargetProperty="(FrameworkElement.RenderTransform).Angle"
To="" Duration="0:0:0.2" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Height="" Margin="0,2,0,0" Width="" x:Name="arrow" RenderTransformOrigin="0.5,0.5">
<Path x:Name="path1"
Data="M8,0.14471819 L8,1.999831 L4,4.2555118 L0,1.9998311 L0,0.1447183 L4,2.400399 z"
Fill="White" Margin="0,4,0,0" Stretch="Fill" StrokeThickness=""
UseLayoutRounding="False" />
<Path x:Name="path2"
Data="M8,0.14471819 L8,1.999831 L4,4.2555118 L0,1.9998311 L0,0.1447183 L4,2.400399 z"
Fill="White" Margin="0,0,0,4" Stretch="Fill" StrokeThickness=""
UseLayoutRounding="False" />
<Grid.RenderTransform>
<RotateTransform Angle="" />
</Grid.RenderTransform>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="ShapeTemplate">
<Grid Background="{Binding Branch, Converter={StaticResource BackgroundSelector}}" Width="" Height="">
<ToolTipService.ToolTip>
<ToolTip Background="#ededed" BorderBrush="Transparent" Padding="">
<ToolTip.Effect>
<DropShadowEffect BlurRadius="" ShadowDepth="" Opacity="0.8" Color="Black" />
</ToolTip.Effect>
<Grid Width="" Height="" Background="#c4c4c4" Margin="">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Width="" VerticalAlignment="Top" Height="" Source="{Binding ImagePath}"
Stretch="Fill" Margin="" />
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column=""
Margin="5 0 0 0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Style="{StaticResource TextBlockNameStyle}" />
</StackPanel>
<TextBlock Text="{Binding JobPosition}" Margin="0 5 0 0"
Style="{StaticResource TextBlockPositionStyle}" />
<TextBlock Text="Seattle" Margin="0 5 0 0" Style="{StaticResource TextBlockStyle}" />
</StackPanel>
<Image Source="/CRS;component/OrgChart/Assets/tooltip_info_back.png" Grid.Row=""
Grid.ColumnSpan="" Stretch="Fill" />
<StackPanel Grid.Row="" Grid.ColumnSpan="" Margin="40 3 0 0">
<TextBlock Text="{Binding Email}" Margin="0 5 0 7"
Style="{StaticResource TextBlockStyle}" />
<TextBlock Text="{Binding Phone}" Margin="0 5 0 7"
Style="{StaticResource TextBlockStyle}" />
<TextBlock Text="Seattle, 607 20th Ave. E., Apt, 24A" Margin="0 5 0 0"
Style="{StaticResource TextBlockStyle}" />
</StackPanel>
</Grid>
</ToolTip>
</ToolTipService.ToolTip>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Width="" Height="" Source="{Binding ImagePath}" Stretch="Fill" Margin="" />
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="" Margin="5 0 0 0">
<TextBlock Text="{Binding JobPosition}" Margin="0 5 0 0"
Style="{StaticResource TextBlockPositionStyle}" />
<TextBlock Text="{Binding Name}" Width="" Style="{StaticResource TextBlockNameStyle}" />
</StackPanel>
<telerik:RadToggleButton Visibility="{Binding HasChildren, Converter={StaticResource Converter}}"
Command="{Binding ToggleVisibilityCommand, Source={StaticResource ViewModel}}"
CommandParameter="{Binding}" Grid.Column="" VerticalAlignment="Top" Width="" Height=""
Margin="0 5 5 0" Style="{StaticResource RadToggleButtonStyle}" />
</Grid>
</DataTemplate>
<Style TargetType="primitives:ConnectorControl" x:Key="ConnectorStyle">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</ResourceDictionary>
ResourcesSL.xaml
2.功能界面:
<scr:BasePage x:Class="CRS.StrategicManage.DecisionMaking"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:scr="clr-namespace:CRS.Common"
mc:Ignorable="d"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:sp="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Title="决策链管理" Loaded="BasePage_Loaded">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width=""></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> <Grid Width="">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions> <telerik:RadTreeView x:Name="rtvOrg" Grid.Row="" Grid.ColumnSpan="" VerticalAlignment="Top" SelectionMode="Single" TabIndex=""/>
</Grid>
<sp:GridSplitter Grid.Column="" Width="" HorizontalAlignment="Stretch" VerticalContentAlignment="Center" Background="#BFBFBF" Margin="1 0"></sp:GridSplitter>
<Grid Grid.Column="" Background="Transparent" DataContext="{StaticResource ViewModel}" x:Name="gdDiagram">
</Grid>
</Grid>
</scr:BasePage>
DecisionMaking.xaml
3. 后台实现
using CRS.Common;
using CRS.CRS_Service;
using CRS.OrgChart;
using CRS.OrgChart.ViewModels;
using CRS.OrgChart.VM;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Telerik.Windows.Controls; namespace CRS.StrategicManage
{
public partial class DecisionMaking : BasePage
{
CRS_Service.CRSServiceClient service = new CRS_Service.CRSServiceClient();
OrgLayoutAlgorithm orgLayoutAlgorithm = new OrgLayoutAlgorithm();
private ViewModel viewModel;
Storyboard collapse, show;
RadTreeViewItem org = null; public DecisionMaking()
{
#region 加载资源 ResourceDictionary newRD = new ResourceDictionary();
newRD.Source = new Uri("/crs;component/StrategicManage/ResourcesSL.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(newRD); #endregion InitializeComponent(); this.viewModel = this.Resources["ViewModel"] as ViewModel;
this.collapse = this.Resources["Collapse"] as Storyboard;
this.show = this.Resources["Show"] as Storyboard;
this.viewModel.NodeVisibilityChanged += this.ViewModel_NodeVisibilityChanged;
} private void BasePage_Loaded(object sender, RoutedEventArgs e)
{
service.GetOrgCompleted += Service_GetOrgCompleted;
service.GetOrgAsync();
} /// <summary>
/// 异步获取角色信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Service_GetOrgCompleted(object sender, CRS_Service.GetOrgCompletedEventArgs e)
{
try
{
this.rtvOrg.Items.Clear(); RadTreeViewItem newItem; foreach (organization i in e.Result.OrderBy(m => m.ID).ToList())
{
newItem = new RadTreeViewItem(); newItem.Header = i.OrgName;
newItem.Tag = i.ID;
newItem.FontSize = ;
newItem.DefaultImageSrc = new BitmapImage(new Uri("/CRS;Component/Images/org.png", UriKind.Relative));
newItem.Foreground = new SolidColorBrush(Colors.Black);
newItem.Margin = new Thickness(, , , );
newItem.IsExpanded = true;
newItem.Click += newItem_Click;
rtvOrg.Items.Add(newItem);
}
rtvOrg.SelectedItem = org = (RadTreeViewItem)rtvOrg.Items[];
ShowChart();
}
catch (Exception)
{
throw;
}
finally
{
service.GetOrgCompleted -= Service_GetOrgCompleted;
}
}
/// <summary>
/// 单击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void newItem_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
try
{
org = (RadTreeViewItem)sender;
ShowChart();
}
catch (Exception ex)
{
RadWindow.Alert(ex.Message);
}
} private void Diagram_SizeChanged(object sender, SizeChangedEventArgs e)
{
RadDiagram d = (RadDiagram)sender;
this.orgLayoutAlgorithm.Layout(d);
d.AutoFit();
} /// <summary>
/// 子节点是否可见
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ViewModel_NodeVisibilityChanged(object sender, VisibilityChangedEventArgs e)
{
RadDiagram diagramControl = (RadDiagram)gdDiagram.Children[];
Node node = sender as Node;
var container = diagramControl.ContainerGenerator.ContainerFromItem(node) as Telerik.Windows.Controls.RadDiagramShape;
Debug.Assert(container != null); if (e.IsVisible)
container.Visibility = System.Windows.Visibility.Visible;
else
container.Visibility = System.Windows.Visibility.Collapsed;
} public void ShowChart()
{
if (org != null)
(this.Resources["ViewModel"] as ViewModel).OrgCategory = org.Tag.ToString(); gdDiagram.Children.Clear(); // 新建一个RadDiagram替换之前的
Telerik.Windows.Controls.RadDiagram diagram = new Telerik.Windows.Controls.RadDiagram();
diagram.SelectionMode = (Telerik.Windows.Diagrams.Core.SelectionMode)SelectionMode.Extended;
diagram.GraphSource = viewModel.GraphSource;
diagram.ShapeTemplate = this.Resources["ShapeTemplate"] as DataTemplate; gdDiagram.Children.Add(diagram);
diagram.SizeChanged += this.Diagram_SizeChanged;
}
}
}
DecisionMaking.xaml。cs
silverlight 控件样式动态绑定的更多相关文章
- 非常精彩的Silverlight 2控件样式
概述 大家是否觉的现在Silverlight 2提供的默认的控件不能满足自己的要求?好在Silverlight的控件可以运用皮肤,微软Silverlight控件的设计者的主管Corrina开发了几套非 ...
- arcgis api for js共享干货系列之二自定义Navigation控件样式风格
arcgis api for js默认的Navigation控件样式风格如下图: 这样的风格不能说不好,各有各的爱好,审美观,这里也不是重点,这里的重点是如何自定义一套自己喜欢的样式风格呢:自己自定义 ...
- WPF中Expander控件样式,ListBox的样式(带checkbox)恢复
Expander控件样式: <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButt ...
- WPF 自定义TabControl控件样式
一.前言 程序中经常会用到TabControl控件,默认的控件样式很普通.而且样式或功能不一定符合我们的要求.比如:我们需要TabControl的标题能够居中.或平均分布:或者我们希望TabContr ...
- arcgis api 3.x for js 共享干货系列之二自定义 Navigation 控件样式风格(附源码下载)
0.内容概览 自定义 Navigation 控件样式风格 源码下载 1.内容讲解 arcgis api 3.x for js 默认的Navigation控件样式风格如下图:这样的风格不能说不好,各有各 ...
- WPF自定义控件(二)の重写原生控件样式模板
话外篇: 要写一个圆形控件,用Clip,重写模板,去除样式引用圆形图片可以有这三种方式. 开发过程中,我们有时候用WPF原生的控件就能实现自己的需求,但是样式.风格并不能满足我们的需求,那么我们该怎么 ...
- QtQuick自定义主题以及控件样式指引
自定义控件样式 请在Qt帮助索引中输入Customizing a Control进行查看 不过实际用下来感觉除非你想自己实现一套效果复杂的UI或是创造一个全新控件,比如:给UI添加模糊.虚化等Shad ...
- [FMX]获取控件样式中的指定项目以便进行调节
[FMX]获取控件样式中的指定项目以便进行调节 2017-03-26 • C++ Builder.Delphi.教程 • 暂无评论 • swish •浏览 650 次 FMX 的样式丰富了我们的设计, ...
- Qt——常用控件样式
下面是我设计.调整.修改的Qt控件样式,仅供参考. Github地址:https://github.com/ikongziming/QtDemo/tree/master/StyleSheetDemo ...
随机推荐
- 规则引擎 - (二)XOM工程
XOM工程即一个Java项目. 新建Java类,添加属性,实现get/set方法.
- wpf 实现 css3 中 boxshadow inset 效果
using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.W ...
- JAVA写接口傻瓜(%)教程(五)
今天主要说一下在URL 中使用?传值的问题.在显式的使用get方法获取特点数据时,一般会通过?传递参数值,sevlert根据参数在数据库中对应的查找内容.所以,SQL语句需要拼接,要加上后面的参数.参 ...
- vue-实例生命周期钩子(不太明白)
每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项}) 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要 ...
- 云计算概述和KVM虚拟化
前言: 近些年一直听着 虚拟化.云计算.公有云.私有云.混合云这些个概念,一直想着....这些概念要用什么技术实现? 一.云计算的概念 1.传统IDC机房面都会临什么问题? 任何新事物都是由需求催生的 ...
- cropper截图不压缩PHP上传裁剪后的图片
cropperjs使用不多说网上都有很详细的介绍如下面: https://blog.csdn.net/lxy4239/article/details/78920979 主要讲下使用的经历 裁剪后图片不 ...
- ClientDataSet应用
最近维护一个项目,里面用到ClientDataSet,由于之前接触ClientDataSet比较少,所以这个星期补了一下关于ClientDataSet的知识,并在此记录下我所了解到的并应用到实际项目中 ...
- baidu-map
1 var map = new BMap.Map("wcp"); // 创建Map实例 2 map.centerAndZoom(new BMap.Point(9.123469591 ...
- python全栈开发笔记---------函数
一 数学定义的函数与python中的函数 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因 ...
- 如何更有效地说服开发接收你的bug?!
来来来,测试小伙伴们,看看以下这张图是不是觉得很熟悉.. 虽然这张图带点戏谑的成分,但确实折射出大部分IT公司测试人员在报bug时,与开发的沟通存在些许问题.如何更有效地说服开发接收你的bug,以下整 ...