让WPF和SL控件同时支持绑定和赋值-阿里云开发者社区

开发者社区> 人工智能> 正文
登录阅读全文

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

简介: 前台: 后台: using System;using System.Collections.Generic;using System.Linq;using System.

前台:


<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;

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

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

Creative Commons License本文基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名http://www.cnblogs.com/luminji(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
人工智能
使用钉钉扫一扫加入圈子
+ 订阅

了解行业+人工智能最先进的技术和实践,参与行业+人工智能实践项目

其他文章
最新文章
相关文章