天天看點

讓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="15"></RowDefinition>

<RowDefinition Height="0"></RowDefinition>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

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

<RowDefinition Height="auto"/>

<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:EventTrigger EventName="Clicked" >

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

</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控件同時支援綁定和指派

本文基于

Creative Commons Attribution 2.5 China Mainland License

釋出,歡迎轉載,演繹或用于商業目的,但是必須保留本文的署名

http://www.cnblogs.com/luminji

(包含連結)。如您有任何疑問或者授權方面的協商,請給我留言。

繼續閱讀