locked
Binding Problem of UserControl with ViewModel RRS feed

  • Question

  • Hi, friends, i have a problem when i try to use the UserControl with ViewModel, i do'nt kown how to solve it, so i come here  hope to find some help....

        i create a userControl so i could use it many times in my MainView...

        but i want to set up some properties of these userControls using data of a relational database, for example, the text of a textblock in the userControl.

        i create a viewModel to respresents these properties so i could use the DataBinding technique....

      then the problem comes:

          how to bind these userControls that i put in the mainView in a way uniforme..., and because of the function that loads data from database  needs a extra

        parameter, how to pass the extra parameter....

     

    the Code is:

    <UserControl
    	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:ed="http://schemas.microsoft.com/expression/2010/drawing"
    	xmlns:local="clr-namespace:SubwayControlSystem"
    	xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    	mc:Ignorable="d"
    	x:Class="SubwayControlSystem.CommonStation"
    	x:Name="UserControl" Height="165.42" Width="234.42" MouseEnter="ShowInfoBox" MouseLeave="HideInfoBox">
    	<UserControl.Resources>
    		<local:CommonStationViewModel x:Key="CommonStationViewModelDataSource" d:IsDataSource="True"/>	 
    	</UserControl.Resources>
    	<Grid x:Name="LayoutRoot" Margin="4,3,4,5" DataContext="{Binding Source={StaticResource CommonStationViewModelDataSource}}">
    		<VisualStateManager.VisualStateGroups>
    			<VisualStateGroup x:Name="AgrandirStateGroup">
    				<VisualState x:Name="MouseEnterState">
    					<Storyboard RepeatBehavior="Forever">
    						<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="ellipse">
    							<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="1.25"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="1.5"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.75"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="2"/>
    						</DoubleAnimationUsingKeyFrames>
    						<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="ellipse">
    							<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="1.25"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="1.5"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1.75"/>
    							<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="2"/>
    						</DoubleAnimationUsingKeyFrames>
    					</Storyboard>
    				</VisualState>
    				<VisualState x:Name="MouseLeaveState"/>
    			</VisualStateGroup>
    		</VisualStateManager.VisualStateGroups>
    		<Grid x:Name="StationGrid" HorizontalAlignment="Left" Height="19.17" VerticalAlignment="Bottom" Width="19.17" Margin="36.666,0,0,8.25">
    			<i:Interaction.Triggers>
    				<i:EventTrigger EventName="MouseEnter">
    					<ei:GoToStateAction StateName="MouseEnterState"/>
    				</i:EventTrigger>
    				<i:EventTrigger EventName="MouseLeave">
    					<ei:GoToStateAction StateName="MouseLeaveState"/>
    				</i:EventTrigger>
    			</i:Interaction.Triggers>
    			<Ellipse x:Name="ellipse" Stroke="#FF7E7575" Fill="#BD7A5B5B" RenderTransformOrigin="0.5,0.5">
    				<Ellipse.RenderTransform>
    					<TransformGroup>
    						<ScaleTransform/>
    						<SkewTransform/>
    						<RotateTransform/>
    						<TranslateTransform/>
    					</TransformGroup>
    				</Ellipse.RenderTransform>
    			</Ellipse>
    			<Ellipse Stroke="Black" Fill="#FF7C6D6D" MouseEnter="ShowInfoBox"/>
    		</Grid>
    		<Grid x:Name="InfoBox" Margin="2,7,8,28.386" Visibility="visible">
    			<Grid x:Name="InfoPanel" Margin="0,0,0,8.367" Background="White">
    				<TextBlock HorizontalAlignment="Left" Height="14.5" Margin="6.335,8.337,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="75.63" FontWeight="Bold" TextDecorations="Underline" Text="车公庄"/>
    				<TextBlock Height="14.5" Margin="103.458,8.337,58.378,0" TextWrapping="Wrap" Text="当前人数:" VerticalAlignment="Top"/>
    				<TextBlock HorizontalAlignment="Right" Height="14.337" Margin="0,8.5,8,0" TextWrapping="Wrap" Text="{Binding Count, Mode=OneWay}" VerticalAlignment="Top" Width="43.42" TextDecorations="Underline"/>
    				<Grid x:Name="LineGrid" Height="18.163" Margin="6.206,33.337,10.961,0" VerticalAlignment="Top">
    					<TextBlock HorizontalAlignment="Left" Margin="0.01,0,0,2.163" TextWrapping="Wrap" Width="55.868" Text="{Binding Line, Mode=OneWay}" Background="{Binding LineGridColor, Mode=OneWay}"/>
    					<GridSplitter HorizontalAlignment="Stretch" Height="2.5" VerticalAlignment="Bottom" Background="{Binding LineGridColor, Mode=OneWay}"/>
    				</Grid>
    				<Grid x:Name="UpRecordGrid" Height="15.334" Margin="6,0,1.878,37.833" VerticalAlignment="Bottom">
    					<TextBlock HorizontalAlignment="Left" Margin="0,0,0,0.834" TextWrapping="Wrap" Text="{Binding UpStation, Mode=OneWay}" Width="55.5"/>
    					<TextBlock Margin="51.69,0,0,0.834" TextWrapping="Wrap" Text="方向" HorizontalAlignment="Left" Width="24.833" Foreground="#FF11746D"/>
    					<TextBlock Margin="90.167,0,102.123,0" TextWrapping="Wrap" Text="始" Background="#FF32B68F" TextAlignment="Center" Foreground="#FFFDFDFD"/>
    					<TextBlock HorizontalAlignment="Right" Margin="0,0.5,56.5,0.334" TextWrapping="Wrap" Text="{Binding UpStartTime, Mode=OneWay}" Width="41.623"/>
    					<TextBlock Margin="0,0,45.123,0" TextWrapping="Wrap" Text="终" Background="#FF32B68F" TextAlignment="Center" Foreground="#FFFDFDFD" HorizontalAlignment="Right" Width="16.252"/>
    					<TextBlock HorizontalAlignment="Right" Margin="0,0,0,0.334" TextWrapping="Wrap" Text="{Binding UpEndTime, Mode=OneWay}" Width="41.623" Height="14.5" VerticalAlignment="Bottom"/>
    				</Grid>
    				<Grid x:Name="DownRecordGrid" Height="15.334" Margin="6,0,1.878,13.154" VerticalAlignment="Bottom">
    					<TextBlock HorizontalAlignment="Left" Margin="0,0,0,0.834" TextWrapping="Wrap" Text="{Binding DownStation, Mode=OneWay}" Width="55.5"/>
    					<TextBlock Margin="51.69,0,0,0.834" TextWrapping="Wrap" Text="方向" HorizontalAlignment="Left" Width="24.833" Foreground="#FF11746D"/>
    					<TextBlock Margin="90.167,0,102.123,0" TextWrapping="Wrap" Text="始" Background="#FF32B68F" TextAlignment="Center" Foreground="#FFFDFDFD"/>
    					<TextBlock HorizontalAlignment="Right" Margin="0,0.5,56.5,0.334" TextWrapping="Wrap" Text="{Binding DownStartTime, Mode=OneWay}" Width="41.623"/>
    					<TextBlock Margin="0,0,45.123,0" TextWrapping="Wrap" Text="终" Background="#FF32B68F" TextAlignment="Center" Foreground="#FFFDFDFD" HorizontalAlignment="Right" Width="16.252"/>
    					<TextBlock HorizontalAlignment="Right" Margin="0,0.5,0,0.334" TextWrapping="Wrap" Text="{Binding DownEndTime, Mode=OneWay}" Width="41.623"/>
    				</Grid>
    			</Grid>
    			<ed:RegularPolygon Fill="White" HorizontalAlignment="Left" Height="8.666" InnerRadius="1" Margin="38.872,0,0,0" PointCount="3" Stretch="Fill" VerticalAlignment="Bottom" Width="8.666" RenderTransformOrigin="0.103,0.517">
    				<ed:RegularPolygon.RenderTransform>
    					<TransformGroup>
    						<ScaleTransform/>
    						<SkewTransform/>
    						<RotateTransform Angle="180"/>
    						<TranslateTransform X="7.675" Y="-0.329"/>
    					</TransformGroup>
    				</ed:RegularPolygon.RenderTransform>
    			</ed:RegularPolygon>
    		</Grid>
    		<Grid x:Name="StartIcon" Margin="26.634,0,0,18.567" Visibility="hidden" HorizontalAlignment="Left" Height="33.298" VerticalAlignment="Bottom" Width="39.162">
    			<Path Data="M23.499995,0.5 L23.564005,0.50347191 23.627999,0.50000095 23.627999,0.50694293 24.239901,0.54013216 C55.055895,2.4345551 53.124468,30.588436 24.193917,63.222172 L23.627999,63.856518 23.627999,64 23.563998,63.928261 23.499995,64 23.499995,63.856518 22.934077,63.222172 C-5.996473,30.588436 -7.927898,2.4345558 22.888094,0.54013312 L23.499995,0.50694382 z" Fill="#BD218D54" Margin="4,6,4.747,0" RenderTransformOrigin="0.255983688440643,0.5" Stretch="Fill" Stroke="#FF7E7575" Height="29.25" VerticalAlignment="Top"/>
    			<TextBlock Margin="13.333,9.768,13.584,9.96" TextWrapping="Wrap" Text="起"/>
    		</Grid>
    		<Grid x:Name="EndIcon" Margin="26.634,0,0,18.567" Visibility="hidden" HorizontalAlignment="Left" Height="33.298" VerticalAlignment="Bottom" Width="39.162">
    			<Path Data="M23.499995,0.5 L23.564005,0.50347191 23.627999,0.50000095 23.627999,0.50694293 24.239901,0.54013216 C55.055895,2.4345551 53.124468,30.588436 24.193917,63.222172 L23.627999,63.856518 23.627999,64 23.563998,63.928261 23.499995,64 23.499995,63.856518 22.934077,63.222172 C-5.996473,30.588436 -7.927898,2.4345558 22.888094,0.54013312 L23.499995,0.50694382 z" Fill="#BD218D54" Margin="4,6,4.747,0" RenderTransformOrigin="0.255983688440643,0.5" Stretch="Fill" Stroke="#FF7E7575" Height="29.25" VerticalAlignment="Top"/>
    			<TextBlock Margin="13.333,9.768,13.584,9.96" TextWrapping="Wrap" Text="终"/>
    		</Grid>
    	</Grid>
    
    
    </UserControl>
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using SubwayControlSystem.DataConnetion;
    using System.Data.OracleClient;
    namespace SubwayControlSystem
    {
    	public class CommonStationViewModel
    	{
    		public CommonStationViewModel()
    		{
    			// 在此点下面插入创建对象所需的代码。
    		}
    		
    		//车站名属性
    		protected string _stationName;
        public string StationName
        {
          get { return _stationName; }
          set
          {
            this._stationName = value;
            NotifyPropertyChanged("StationName");
          }
        }
        
    		//当前人数属性
        protected int _count;
        public int Count
        {
          get { return _count; }
          set
          {
            this._count = value;
            NotifyPropertyChanged("Count");
          }
        }
        
    		//线路名称属性
        protected string _line;
        public string Line
        {
          get { return _line; }
          set
          {
            _line = value;
            NotifyPropertyChanged("Line");
          }
        }
        
    		//线路标签和分隔符属性
        protected System.Windows.Media.Brush _lineGridColor;
        public System.Windows.Media.Brush LineGridColor
        {
          get { return _lineGridColor; }
          set
          {
            _lineGridColor = value;
            NotifyPropertyChanged("LineGridColor");
          }
        }
        
    		//上行方向的车站名
        protected string _upStation;
        public string UpStation
        {
          get { return _upStation; }
          set
          {
            this._upStation = value;
            NotifyPropertyChanged("UpStation");
          }
        }
        
    		//下行方向的车站名
        protected string _downStation;
        public string DownStation
        {
          get { return _downStation; }
          set
          {
            this._downStation = value;
            NotifyPropertyChanged("DownStation");
          }
        }
        
    		//上行方向的首班车时间
        protected string _upStartTime;
        public string UpStartTime
        {
          get { return _upStartTime; }
          set
          {
            this._upStartTime = value;
            NotifyPropertyChanged("UpStartTime");
          }
        }
        
    		//下行方向的末班车时间
        protected string _downStartTime;
        public string DownStartTime
        {
          get { return _downStartTime; }
          set
          {
            this._downStartTime = value;
            NotifyPropertyChanged("DownStartTime");
          }
        }
        
    		//上行方向的末班车时间
        protected string _upEndTime;
        public string UpEndTime
        {
          get { return _upEndTime; }
          set
          {
            this._upEndTime = value;
            NotifyPropertyChanged("UpEndTime");
          }
        }
        
    		//下行方向的末班车时间
        protected string _downEndTime;
        public string DownEndTime
        {
          get { return _downEndTime; }
          set
          {
            this._downEndTime = value;
            NotifyPropertyChanged("DownEndTime");
          }
        }
    		
    		#region INotifyPropertyChanged
    		public event PropertyChangedEventHandler PropertyChanged;
    
    		private void NotifyPropertyChanged(String info)
    		{
    			if (PropertyChanged != null)
    			{
    				PropertyChanged(this, new PropertyChangedEventArgs(info));
    			}
    		}
    		#endregion
    	}
    
    Wednesday, April 20, 2011 2:04 PM

Answers

All replies