让WPF和SL控件同时支持绑定和赋值
2013-11-27 14:01
363 查看
前台:
后台:
注意:
SingleChoice_Loaded
和
set中的
(tbb.DataContext as SingleChoiceViewModel).Choice = value;
第一句是为了绑定的时候。
下面那句是为了直接赋值。
<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;
第一句是为了绑定的时候。
下面那句是为了直接赋值。
相关文章推荐
- 让WPF和SL控件同时支持绑定和赋值
- WPF 支持集合绑定的控件
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
- WPF实现支持Command绑定的ComboBox控件
- CYQ.Data 支持WPF相关的数据控件绑定.Net获取iis版本
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
- silverlight(wpf/e) v1不支持控件?
- 数据绑定到任何WPF的图表控件Column & Bar Chart
- WPF&Silverlight之XamlReader动态创建控件并绑定
- Windows Presentation Foundation(WPF)中的数据绑定(控件与控件值的绑定)
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- WPF 数据控件绑定数据与 ASP.NET数据控件绑定数据的区别
- WPF 控件之ComboBox绑定
- wpf控件设计时支持(3)
- Windows Presentation Foundation(WPF)中的数据绑定(使用XmlDataProvider作控件绑定)
- WPF学习(1):控件之间的数据绑定
- InteropBitmap指定内存,绑定WPF的Imag控件时刷新问题。
- WPF中控件绑定某类的成员变量