让WPF和SL控件同时支持绑定和赋值

简介:

前台:

复制代码
 
  
< UserControl x:Name ="msl"
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:Control
="clr-namespace:Sl.MELearning.EnterpriseLibrary.Controls.ControlBase"
xmlns:vm
="clr-namespace:Sl.MELearning.EnterpriseLibrary.Controls.ViewModel"
xmlns:cv
="clr-namespace:Sl.MELearning.EnterpriseLibrary.Controls.Converter"
xmlns:i
="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei
="http://schemas.microsoft.com/expression/2010/interactions"
x:Class
="Sl.MELearning.EnterpriseLibrary.Controls.SingleChoice"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth ="400" >
< UserControl.Resources >
< ResourceDictionary >
< ResourceDictionary.MergedDictionaries >
< ResourceDictionary Source ="Styles/DictionaryChoice.xaml" />
</ ResourceDictionary.MergedDictionaries >
</ ResourceDictionary >
</ UserControl.Resources >
< Grid x:Name ="LayoutRoot" Background ="White" >
< Grid.RowDefinitions >
< RowDefinition Height ="auto" ></ RowDefinition >
< RowDefinition Height ="auto" ></ RowDefinition >
< RowDefinition Height ="15" ></ RowDefinition >
< RowDefinition Height ="0" ></ RowDefinition >
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="40" />
< ColumnDefinition Width ="40" />
< ColumnDefinition Width ="*" />
</ Grid.ColumnDefinitions >


< TextBlock Text =" {Binding Choice.IndexInPaper, Mode=TwoWay, ElementName=msl} " Grid.Row ="0" Grid.Column ="0" Width ="30" Height ="30" Margin ="16,12,0,0" >
</ TextBlock >

< Control:PaperRichText x:Name ="prtQuesiton" Grid.Row ="0" Grid.Column ="1" Grid.ColumnSpan ="2" Xaml =" {Binding Choice.ChoiceQuestion, Mode=TwoWay, ElementName=msl} " IsReadOnly ="True" BorderShowed ="False" ScrollViewer.HorizontalScrollBarVisibility ="Hidden" ScrollViewer.VerticalScrollBarVisibility ="Hidden" Margin ="5,5,5,5" >
</ Control:PaperRichText >

< ListBox x:Name ="LbSingleSelection" Grid.Row ="1" x:FieldModifier ="public" ItemsSource =" {Binding Choice.ChoiceStems, ElementName=msl} " HorizontalAlignment ="Stretch" VerticalAlignment ="Stretch" ScrollViewer.HorizontalScrollBarVisibility ="Hidden" ScrollViewer.VerticalScrollBarVisibility ="Hidden" Grid.ColumnSpan ="3" Width ="Auto" ItemContainerStyle =" {StaticResource ListBoxItemStyle1} " BorderThickness ="0" >
< ListBox.ItemTemplate >
< DataTemplate >
< Grid >
< Grid.RowDefinitions >
< RowDefinition Height ="auto" />
</ Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition Width ="40" />
< ColumnDefinition Width ="*" />
</ Grid.ColumnDefinitions >

< Border Grid.Column ="0" Grid.Row ="0" Width ="25" Height ="25" Cursor ="Hand" CornerRadius ="25" BorderThickness ="2" Background =" {Binding Selected, Converter={StaticResource borderBackgroundConvert}} " BorderBrush =" {Binding Selected, Converter={StaticResource borderColorConvert}} " >
< TextBlock Text =" {Binding Path=StemABCDShowed, Mode=TwoWay} " FontSize ="12" Width ="25" Height ="25" Cursor ="Hand" Foreground =" {Binding Selected, Converter={StaticResource borderColorConvert}} " Padding ="8,3,0,0" HorizontalAlignment ="Center" VerticalAlignment ="Center" />
< i:Interaction.Triggers >
< i:EventTrigger EventName ="MouseLeftButtonDown" >
< i:InvokeCommandAction Command =" {Binding DataContext.Clicked, Mode=TwoWay, ElementName=tbb} " CommandParameter =" {Binding} " >
</ i:InvokeCommandAction >
</ i:EventTrigger >
</ i:Interaction.Triggers >
</ Border >

< Control:PaperRichText x:Name ="prtListItem" Grid.Column ="1" Grid.Row ="0" Xaml =" {Binding Path=Stem} " IsReadOnly ="True" BorderShowed ="False" Cursor ="Hand" Width =" {Binding Width, ElementName=msl, Converter={StaticResource choiceWidthConverter}} " >
< i:Interaction.Triggers >
< i:EventTrigger EventName ="Clicked" >
< i:InvokeCommandAction Command =" {Binding DataContext.Clicked, Mode=TwoWay, ElementName=tbb} " CommandParameter =" {Binding} " >
</ i:InvokeCommandAction >
</ i:EventTrigger >
</ i:Interaction.Triggers >
</ Control:PaperRichText >

</ Grid >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
< Rectangle Height ="10" VerticalAlignment ="bottom" Grid.Row ="2" Grid.ColumnSpan ="3" Style =" {StaticResource ChoiceRectangle} " >
</ Rectangle >
< TextBlock Height ="0" Grid.Row ="3" Grid.ColumnSpan ="3" x:Name ="tbb" >
< TextBlock.DataContext >
< vm:SingleChoiceViewModel x:Name ="singleChoiceViewModel" ></ vm:SingleChoiceViewModel >
</ TextBlock.DataContext >
</ TextBlock >
</ Grid >
</ UserControl >
复制代码

后台:

复制代码
 
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Sl.MELearning.EnterpriseLibrary.Controls.Core;
using Sl.MELearning.EnterpriseLibrary.Controls.ViewModel;
using Sl.MELearning.EnterpriseLibrary.Controls.UIModel;

namespace Sl.MELearning.EnterpriseLibrary.Controls
{
public partial class SingleChoice : UserControl
{
public SingleChoice()
{
InitializeComponent();
this .Loaded += new RoutedEventHandler(SingleChoice_Loaded);

}

void SingleChoice_Loaded( object sender, RoutedEventArgs e)
{
(tbb.DataContext
as SingleChoiceViewModel).Choice = Choice;
}

public static readonly DependencyProperty ChoiceProperty = DependencyProperty.Register( " Choice " , typeof (Choice), typeof (SingleChoice), new PropertyMetadata( null ));
public Choice Choice
{
get
{
return (Choice)GetValue(ChoiceProperty);
}
set
{
SetValue(ChoiceProperty, value);
(tbb.DataContext
as SingleChoiceViewModel).Choice = value;
}
}

}


}
复制代码

注意:

SingleChoice_Loaded

set中的

(tbb.DataContext as SingleChoiceViewModel).Choice = value;

第一句是为了绑定的时候。

下面那句是为了直接赋值。

本文转自最课程陆敏技博客园博客,原文链接:http://www.cnblogs.com/luminji/archive/2011/06/06/2073821.html,如需转载请自行联系原作者
相关文章
|
11天前
|
C# 开发者 Windows
基于Material Design风格开源、易用、强大的WPF UI控件库
基于Material Design风格开源、易用、强大的WPF UI控件库
|
4月前
|
C#
浅谈WPF之装饰器实现控件锚点
使用过visio的都知道,在绘制流程图时,当选择或鼠标移动到控件时,都会在控件的四周出现锚点,以便于修改大小,移动位置,或连接线等,那此功能是如何实现的呢?在WPF开发中,想要在控件四周实现锚点,可以通过装饰器来实现,今天通过一个简单的小例子,简述如何在WPF开发中,应用装饰器,仅供学习分享使用,如有不足之处,还请指正。
59 1
|
8月前
|
C# Windows
WPF技术之图形系列Polygon控件
WPF Polygon是Windows Presentation Foundation (WPF)框架中的一个标记元素,用于绘制多边形形状。它可以通过设置多个点的坐标来定义多边形的形状,可以绘制任意复杂度的多边形。
451 0
|
8月前
|
C# Windows
WPF技术之RichTextBox控件
WPF RichTextBox是Windows Presentation Foundation (WPF)中提供的一个强大的文本编辑控件,它可以显示富文本格式的文本,支持多种文本处理操作。
345 0
|
4月前
|
前端开发 C# 容器
浅谈WPF之控件拖拽与拖动
使用过office的visio软件画图的小伙伴都知道,画图软件分为两部分,左侧图形库,存放各种图标,右侧是一个画布,将左侧图形库的图标控件拖拽到右侧画布,就会生成一个新的控件,并且可以自由拖动。那如何在WPF程序中,实现类似的功能呢?今天就以一个简单的小例子,简述如何在WPF中实现控件的拖拽和拖动,仅供学习分享使用,如有不足之处,还请指正。
106 2
|
8月前
|
数据挖掘 数据处理 C#
WPF技术之DataGrid控件
WPF DataGrid是一种可以显示和编辑数据的界面控件。它可以作为表格形式展示数据,支持添加、删除、修改、排序和分组操作。
175 0
|
12天前
|
C# 开发者 C++
一套开源、强大且美观的WPF UI控件库
一套开源、强大且美观的WPF UI控件库
127 0
|
5月前
|
算法 C# UED
浅谈WPF之控件模板和数据模板
WPF不仅支持传统的Windows Forms编程的用户界面和用户体验设计,同时还推出了以模板为核心的新一代设计理念。在WPF中,通过引入模板,将数据和算法的“内容”和“形式”进行解耦。模板主要分为两大类:数据模板【Data Template】和控件模板【Control Template】。
94 8
|
8月前
|
定位技术 C# UED
WPF技术之ScrollViewer控件
WPF ScrollViewer是WPF中常用的一个控件,它提供了滚动视图的功能,可用于显示超出容器可视区域的内容。ScrollViewer通常用于容纳大量内容的控件,以在有限的空间内显示这些内容,并允许用户通过滚动来查看隐藏的部分。
692 0
|
8月前
|
前端开发 C#
WPF技术之ContentControl 控件
ContentControl 是 WPF 中的一个常见控件,用于显示单个内容元素。它可以包含任意类型的内容,包括文本、图像、控件等。
764 0