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

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

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

  • 相关阅读:
    C++ 类的内存分布
    Hadoop集群安装配置教程_Hadoop2.6.0_Ubuntu/CentOS
    Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04
    linux 入门教程
    linux shell 常用基本语法
    linux系统的7种运行级别
    Linux学习之CentOS6下Mysql数据库的安装与配置
    二叉树方面的问题
    先贴出代码C++ 中的单例模式
    C++11 中的线程、锁和条件变量
  • 原文地址:https://www.cnblogs.com/luminji/p/2073821.html
Copyright © 2020-2023  润新知