Click here to Skip to main content
15,897,518 members
Home / Discussions / WPF
   

WPF

 
QuestionBind to Single Element in Itemssource Using XAML Pin
C.J.7-Jan-10 5:42
C.J.7-Jan-10 5:42 
AnswerRe: Bind to Single Element in Itemssource Using XAML Pin
Abhinav S7-Jan-10 5:59
Abhinav S7-Jan-10 5:59 
GeneralRe: Bind to Single Element in Itemssource Using XAML Pin
C.J.7-Jan-10 6:04
C.J.7-Jan-10 6:04 
GeneralRe: Bind to Single Element in Itemssource Using XAML Pin
Abhinav S7-Jan-10 6:16
Abhinav S7-Jan-10 6:16 
GeneralRe: Bind to Single Element in Itemssource Using XAML Pin
C.J.7-Jan-10 6:47
C.J.7-Jan-10 6:47 
QuestionWPF Datagrid Style Pin
reibor6-Jan-10 10:12
reibor6-Jan-10 10:12 
AnswerRe: WPF Datagrid Style Pin
Mark Salsbery6-Jan-10 12:37
Mark Salsbery6-Jan-10 12:37 
QuestionArgghh my controls are disabled and cannot figure out why!!! Pin
Michael Eber6-Jan-10 9:26
Michael Eber6-Jan-10 9:26 
I have a Silverlight 3.0 application which has the following:

listbox     listbox     grid (editContainer)
Customers   Locations       3 comboboxes
                            2 textboxes
                            2 buttons


At startup the customer listbox is populated -- nothing else! At this point all of the controls in the editContainer are unaccessible...as if they are in a disabled state. Frown | :(
When a customer is selected the customer Locations list is populated, 2 comboboxes are populated. I've walked through this in debug mode and confirmed that the comboboxes are indeed getting data. The combo boxes are using code from another site that inherits the base silverlight code and adds the population logic when a database item has the key value instead of the text value in the combo box.

When I get the message that I can now edit data, everything in the editContainer is still in a disabled state. Frown | :( ComboBoxes don't drop down, cannot get my cursor entered into the textbox, and my buttons don't fire events. I initially thought it was because I added code to set the selected index on the combo boxes to -1, but even after commenting all that out I still have read-only controls.

here is the xaml:

<navigation:Page 
           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"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

           xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" 
           xmlns:DatabaseManager_Silverlight_MasterService="clr-namespace:DatabaseManager.Silverlight.MasterService" 
           xmlns:DatabaseManager_Silverlight="clr-namespace:DatabaseManager.Silverlight" 
           xmlns:mycompany_Silverlight_Controls="clr-namespace:mycompany.Silverlight.Controls;assembly=mycompany.Silverlight.Controls" 
           x:Class="DatabaseManager.Silverlight.Views.ManageCustomerLocations"
           d:DesignWidth="800" d:DesignHeight="600"
           Title="ManageCustomerLocations Page">
    <Grid x:Name="LayoutRoot" Background="#FFF8F8DE">
    	<Grid x:Name="editContainer" Margin="8,20,8,6">

    		<ListBox x:Name="customerList" HorizontalAlignment="Left" Margin="0,16,0,29" Width="183" SelectionChanged="CustomerSelectedEventHandler" DisplayMemberPath="CustomerName" />
    		<TextBox x:Name="message" Height="23" VerticalAlignment="Bottom" IsReadOnly="True" Text="{}{messages will appear here}" TextWrapping="Wrap"/>
    		<ListBox x:Name="customerLocations" HorizontalAlignment="Left" Margin="187,16,0,29" Width="100" SelectionChanged="CustomerLocationSelectedEventHandler" DisplayMemberPath="LocationID" />
    		<dataInput:Label HorizontalAlignment="Left" Margin="38,0,0,0" VerticalAlignment="Top" Width="72" Content="Customers" FontWeight="Bold" Foreground="#FF0D0D39" RenderTransformOrigin="1.153,0.562"/>
    		<dataInput:Label HorizontalAlignment="Left" Margin="204,0,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.567,1.562" Width="63" Content="Locations" FontWeight="Bold" Foreground="#FF0D0D39"/>
    		<Grid x:Name="locationContainer" Margin="291,16,0,29" IsHitTestVisible="False">
    			<Grid.DataContext>
    				<DatabaseManager_Silverlight_MasterService:CustomerLocation/>
    			</Grid.DataContext>
    			<Button x:Name="bmcKeyComputation" Margin="21,0,0,133" Content="Compute BMC Key" Click="ComputeBmcKeyEventHandler" Height="25" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="132"/>
    			<Button x:Name="submitChanges" Margin="21,0,31,65" VerticalAlignment="Bottom" Content="Create Location" Click="PostLocationEventHandler" ToolTipService.ToolTip="This button will create/update the location record."/>
    			<dataInput:Label HorizontalAlignment="Left" Margin="21,0,0,181" VerticalAlignment="Bottom" Content="Location Identity:" d:LayoutOverrides="HorizontalAlignment"/>
    			<TextBox x:Name="locationIdentity" HorizontalAlignment="Right" Margin="0,0,31,173" TextWrapping="Wrap" VerticalAlignment="Bottom" DataContext="{Binding LocationID, Mode=TwoWay, UpdateSourceTrigger=Explicit}" ToolTipService.ToolTip="This is the name for the location (office number, cage ID, etc)"/>
    			<TextBox x:Name="bmcKey" HorizontalAlignment="Right" Margin="0,0,31,134" TextWrapping="Wrap" VerticalAlignment="Bottom" DataContext="{Binding BmcLocKey, Mode=TwoWay, UpdateSourceTrigger=Explicit}" IsReadOnly="True" ToolTipService.ToolTip="The resulting BMC Key generated from the previous location data"/>
    			<dataInput:Label Height="20" VerticalAlignment="Top" Content="Building :" HorizontalAlignment="Left" Width="71" Margin="21,63,0,0"/>
    			<dataInput:Label Height="21" Margin="21,104,0,0" VerticalAlignment="Top" Content="Data Center :" HorizontalAlignment="Left" Width="80"/>
    			<dataInput:Label Height="19" Margin="21,146,0,0" VerticalAlignment="Top" Content="Location Type :" HorizontalAlignment="Left" Width="89"/>

    			<mycompany_Silverlight_Controls:ComboBoxClassic x:Name="buildingList" Margin="123,63,31,0" VerticalAlignment="Top" DisplayMemberPath="{Binding Building, Mode=TwoWay, UpdateSourceTrigger=Default}" ToolTipService.ToolTip="Identifies the starting point for this location"/>
    			<mycompany_Silverlight_Controls:ComboBoxClassic x:Name="dataCenterList" Margin="123,104,31,0" VerticalAlignment="Top" ToolTipService.ToolTip="Provides the secondary component of the location -- this changes based on 'building'" DataContext="{Binding DataCenter, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
    			<mycompany_Silverlight_Controls:ComboBoxClassic x:Name="locationTypeList" Margin="123,146,31,0" VerticalAlignment="Top" ToolTipService.ToolTip="For true datacenter locations, this is the type of location the customer contracted" DataContext="{Binding LocationType, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>

    		</Grid>

    	</Grid>
    </Grid>
</navigation:Page>


It's alot of code, but here is the code:

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 System.Windows.Navigation;

using DatabaseManager.Silverlight.MasterService;

namespace DatabaseManager.Silverlight.Views
{
	public partial class ManageCustomerLocations : Page
	{

		#region Page initialization logic

		public ManageCustomerLocations( )
		{

			InitializeComponent( );
			buildingList.DisplayMemberPath = "BuildingName";
			buildingList.SelectedValuePath = "BuildingIdentity";
			buildingList.BindingValidationError+=new EventHandler<ValidationErrorEventArgs>(buildingList_BindingValidationError);

			dataCenterList.DisplayMemberPath = "DataCenterText";
			dataCenterList.SelectedValuePath = "DataCenterIdentity";
			dataCenterList.BindingValidationError+=new EventHandler<ValidationErrorEventArgs>(dataCenterList_BindingValidationError);

			locationTypeList.DisplayMemberPath = "LocationName";
			locationTypeList.SelectedValuePath = "LocationType";
			locationTypeList.BindingValidationError+=new EventHandler<ValidationErrorEventArgs>(locationTypeList_BindingValidationError);

			locationContainer.DataContext = new CustomerLocation( );
		}

		void locationTypeList_BindingValidationError( object sender, ValidationErrorEventArgs e )
		{
			MessageBox.Show( e.Error.Exception.Message );
		}

		void buildingList_BindingValidationError( object sender, ValidationErrorEventArgs e )
		{
			MessageBox.Show( e.Error.Exception.Message );
		}

		void dataCenterList_BindingValidationError( object sender, ValidationErrorEventArgs e )
		{
			MessageBox.Show( e.Error.Exception.Message );
		}
		bool InUpdateState = false;
		// Executes when the user navigates to this page.
		protected override void OnNavigatedTo( NavigationEventArgs e )
		{
			PopulateCustomerList( );
		}

		private void PopulateCustomerList( )
		{
			DataReflectorClient client = new DataReflectorClient( );
			client.OpenAsync( );
			client.GetActiveCustomersCompleted += new EventHandler<GetActiveCustomersCompletedEventArgs>( ActiveCustomersEventHandler );
			client.GetActiveCustomersAsync( );
			client.CloseAsync( );
			message.Text = "Loading active customers at this moment...please wait.";
		}

		#endregion

		#region Database Completion Event Handlers


		void GetCustomerLocationsCompletedEventHandler( object sender, GetCustomerLocationsCompletedEventArgs e )
		{
			customerLocations.ItemsSource = e.Result;
			if ( e.Error != null )
				SetWarningText( e.Error.Message );
			else
				SetEndingText( "Customer locations loaded successfully." );
			customerLocations.UpdateLayout( );
		}

		void ActiveCustomersEventHandler( object sender, GetActiveCustomersCompletedEventArgs e )
		{
			this.customerList.ItemsSource = e.Result;
			SetEndingText( "You may now choose the customer you want to work on." );
		}


		void GetLocationsCompleteEventHandler( object sender, GetLocationTypesCompletedEventArgs e )
		{
			locationTypeList.ItemsSource = e.Result;
			SetEndingText( "Dropdown boxes populated." );
			locationTypeList.UpdateLayout( );
		}

		void GetBuildingsCompleteEventHandler( object sender, GetBuildingEnumeratorsCompletedEventArgs e )
		{
			buildingList.ItemsSource = e.Result;
			SetEndingText( "Dropdown boxes populated." );
			buildingList.UpdateLayout( );
		}


		void LoadDataCenterEventHandler( object sender, GetDatacenterEnumeratorsCompletedEventArgs e )
		{
			dataCenterList.ItemsSource = e.Result;
			SetEndingText( "You may now select which datacenter the customer location is at." );
			dataCenterList.UpdateLayout( );
		}


		void HandleUpdateCompleteEventHandler( object sender, ManageCustomerLocationCompletedEventArgs e )
		{
			MessageResults results = e.Result as MessageResults;
			if ( results == null )
				SetWarningText( String.Format( "Unexpected result sent to completion event.  Type of result was : {0}", e.Result.GetType( ).ToString( ) ) );
			else if ( results.Status.Equals( 1 ) )
			{
				message.Text = String.Format( "Results:  {0}", results.Message );
			}
			else
				message.Text = String.Format( "Error: {0}  Reason : {1}.....ooops.", results.ErrorMessage, results.Reason );
			selectedLocation = null;
			//customerList.SelectedIndex = -1;
		}

		#endregion

		#region Selection Event Handler Implementations

		private void CustomerSelectedEventHandler( object sender, SelectionChangedEventArgs e )
		{
			customerLocations.ItemsSource = null;
			if ( customerList.SelectedIndex < 0 )
			{
				return;
			}

			CustomerMaster master = customerList.SelectedItem as CustomerMaster;
			if ( master == null )
			{
				SetWarningText( "There is a problem getting access to the selected customer." );
				return;
			}

			DataReflectorClient client = new DataReflectorClient( );
			client.OpenAsync( );

			client.GetCustomerLocationsCompleted += new EventHandler<GetCustomerLocationsCompletedEventArgs>( GetCustomerLocationsCompletedEventHandler );
			client.GetCustomerLocationsAsync( master.CustomerIdentity );
			InUpdateState = false;

			SetWarningText( "Populating dropdown boxes.  Please wait...." );



			client.GetBuildingEnumeratorsCompleted += new EventHandler<GetBuildingEnumeratorsCompletedEventArgs>( GetBuildingsCompleteEventHandler );
			client.GetBuildingEnumeratorsAsync( );
			client.GetLocationTypesCompleted += new EventHandler<GetLocationTypesCompletedEventArgs>( GetLocationsCompleteEventHandler );
			client.GetLocationTypesAsync( );

			client.CloseAsync( );

		}

		CustomerLocation selectedLocation = null;

		private void CustomerLocationSelectedEventHandler( object sender, System.Windows.Controls.SelectionChangedEventArgs e )
		{
			if ( customerLocations.SelectedIndex < 0 )
			{
				//buildingList.SelectedIndex = -1;
				//dataCenterList.SelectedIndex = -1;
				//locationTypeList.SelectedIndex = -1;
				//locationIdentity.Text = string.Empty;
				//bmcKey.Text = string.Empty;
			}
			else
			{
				InUpdateState = true;
				selectedLocation = customerLocations.SelectedItem as CustomerLocation;
				locationContainer.DataContext = selectedLocation;
				SetEndingText( String.Format( "Chosen location {0} is now loaded into the editor.", selectedLocation.LocationID) );
			}

		}


		private void BuildingSelectedEventHandler( object sender, SelectionChangedEventArgs e )
		{
			if ( buildingList.SelectedIndex < 0 ) return;

			SetWarningText( "Getting the datacenter locations for selected building location." );
			DataReflectorClient client = new DataReflectorClient( );
			client.OpenAsync( );
			client.GetDatacenterEnumeratorsCompleted += new EventHandler<GetDatacenterEnumeratorsCompletedEventArgs>( LoadDataCenterEventHandler );
			client.GetDatacenterEnumeratorsAsync( ( ( BuildingEnumerator )buildingList.SelectedItem ).BuildingIdentity );
			client.CloseAsync( );
		}

		private void DataCenterSelectedEventHandler( object sender, System.Windows.Controls.SelectionChangedEventArgs e )
		{
			if ( dataCenterList.SelectedIndex < 0 ) return;

			SetEndingText( "You may now select the location type if applicable to this location." );
		}

		private void locationTypeList_SelectionChanged( object sender, SelectionChangedEventArgs e )
		{
			// TODO: Add event handler implementation here.
		}

		#endregion

		#region Button Event handler implementations

		private void ComputeBmcKeyEventHandler(object sender, System.Windows.RoutedEventArgs e)
		{
			message.Text = string.Empty;
			BuildingEnumerator building = buildingList.SelectedItem as BuildingEnumerator;
			if ( building == null )
				message.Text = "You appear to have not selected a building yet.  You need a building location to compute a BMC Key.";
			else if ( locationIdentity.Text.Length.Equals( 0 ) )
				message.Text = "You appear to have not provided a location name.  Enter the location name before computing the BMC Key.";
			else
				bmcKey.Text = String.Concat( building.BuildingName.Substring( 0, 5 ), locationIdentity.Text );
		}

		private void PostLocationEventHandler(object sender, System.Windows.RoutedEventArgs e)
		{
			bool NoErrorsExist = true;
			BuildingEnumerator building=null;
			message.Text = string.Empty;

			if ( buildingList.SelectedItem == null )
			{
				message.Text += "No building selected.  ";
				NoErrorsExist = false;
			}
			else
				building = (BuildingEnumerator)buildingList.SelectedItem;
			if ( dataCenterList.SelectedItem == null )
			{
				message.Text += "No data center selected.  ";
				NoErrorsExist = false;
			}
			
			if ( locationTypeList.SelectedItem==null && building != null && building.BuildingName != "Exacent" && building.BuildingName!="Office" )
			{
				message.Text += "No location type selected.  ";
				NoErrorsExist = false;
			}
			if ( locationIdentity.Text.Length.Equals( 0 ) )
			{
				message.Text = "No location name given.  ";
				NoErrorsExist = false;
			}
			if ( bmcKey.Text.Length.Equals( 0 ) && building != null && building.BuildingName != "Exacent" && building.BuildingName != "Office" )
			{
				message.Text = "BMC Key has not been generated.  ";
				NoErrorsExist = false;
			}
			if ( NoErrorsExist )
			{
				DataReflectorClient client = new DataReflectorClient( );
				CustomerLocation newLocation = new CustomerLocation( );
				if ( customerLocations.SelectedIndex > 0 )
					newLocation = ( CustomerLocation )customerLocations.SelectedItem;

				client.OpenAsync( );

				if ( InUpdateState )
				{
					if ( newLocation.Building != ( ( BuildingEnumerator )buildingList.SelectedItem ).BuildingIdentity )
						newLocation.Building = ( ( BuildingEnumerator )buildingList.SelectedItem ).BuildingIdentity;
					if ( newLocation.DataCenter != ( ( DataCenterEnumerator )dataCenterList.SelectedItem ).DataCenterIdentity )
						newLocation.DataCenter = ( ( DataCenterEnumerator )dataCenterList.SelectedItem ).DataCenterIdentity;
					if ( newLocation.LocationType.HasValue && newLocation.LocationType.Value != ( ( LocationEnumerator )locationTypeList.SelectedItem ).LocationType )
						newLocation.LocationType = ( ( LocationEnumerator )locationTypeList.SelectedItem ).LocationType;
					if ( newLocation.LocationID != locationIdentity.Text )
						newLocation.LocationID = locationIdentity.Text;
					if ( newLocation.BmcLocKey != bmcKey.Text )
						newLocation.BmcLocKey = bmcKey.Text;
					
					client.ManageCustomerLocationCompleted += new EventHandler<ManageCustomerLocationCompletedEventArgs>( HandleUpdateCompleteEventHandler );
					client.ManageCustomerLocationAsync( newLocation, RequestType.Update );
					client.CloseAsync( );
				}
				else
				{
					CustomerLocation newlocation = new CustomerLocation();
					newlocation.LocationIdentity = Guid.NewGuid( );
					newlocation.CustomerIdentity = ( ( CustomerMaster )customerList.SelectedItem ).CustomerIdentity;
					newlocation.Building = ( ( BuildingEnumerator )buildingList.SelectedItem ).BuildingIdentity;
					newlocation.DataCenter = ( ( DataCenterEnumerator )dataCenterList.SelectedItem ).DataCenterIdentity;
					newlocation.LocationType = locationTypeList.SelectedIndex >= 0 ? ( ( LocationEnumerator )locationTypeList.SelectedItem ).LocationType : new Nullable<int>( );
					newlocation.LocationID = locationIdentity.Text;
					newlocation.BmcLocKey = bmcKey.Text;

					client.ManageCustomerLocationCompleted += new EventHandler<ManageCustomerLocationCompletedEventArgs>( HandleUpdateCompleteEventHandler );
					client.ManageCustomerLocationAsync( newlocation, RequestType.Create );
					client.CloseAsync( );
				}
			}
			else
			{
				message.Text += "Please correct errors.";
				return;
			}
		}

		#endregion

		void SetWarningText( string text )
		{
			SolidColorBrush brush = new SolidColorBrush( Colors.Red );
			message.Foreground = brush;
			message.Text = text;
		}
		void SetEndingText( string text )
		{
			SolidColorBrush brush = new SolidColorBrush( Colors.Black );
			message.Foreground = brush;
			message.Text = text;
		}


	}
}

AnswerRe: Argghh my controls are disabled and cannot figure out why!!! Pin
Ian Shlasko7-Jan-10 3:09
Ian Shlasko7-Jan-10 3:09 
GeneralRe: Argghh my controls are disabled and cannot figure out why!!! Pin
Michael Eber7-Jan-10 7:47
Michael Eber7-Jan-10 7:47 
GeneralRe: Argghh my controls are disabled and cannot figure out why!!! Pin
Ian Shlasko7-Jan-10 10:35
Ian Shlasko7-Jan-10 10:35 
Questionhow to navigate from 1 page to another Pin
hotthoughtguy6-Jan-10 8:18
hotthoughtguy6-Jan-10 8:18 
AnswerRe: how to navigate from 1 page to another Pin
Mark Salsbery6-Jan-10 8:47
Mark Salsbery6-Jan-10 8:47 
AnswerRe: how to navigate from 1 page to another Pin
April Fans6-Jan-10 19:10
April Fans6-Jan-10 19:10 
Questionsubclassing a treeviewitem Pin
hb521342146-Jan-10 4:21
hb521342146-Jan-10 4:21 
AnswerRe: subclassing a treeviewitem Pin
AspDotNetDev6-Jan-10 20:53
protectorAspDotNetDev6-Jan-10 20:53 
GeneralRe: subclassing a treeviewitem Pin
hb521342147-Jan-10 6:26
hb521342147-Jan-10 6:26 
QuestionRe: subclassing a treeviewitem Pin
AspDotNetDev7-Jan-10 13:44
protectorAspDotNetDev7-Jan-10 13:44 
AnswerRe: subclassing a treeviewitem Pin
hb521342147-Jan-10 15:07
hb521342147-Jan-10 15:07 
GeneralRe: subclassing a treeviewitem Pin
hb521342147-Jan-10 15:14
hb521342147-Jan-10 15:14 
GeneralRe: subclassing a treeviewitem Pin
AspDotNetDev7-Jan-10 15:36
protectorAspDotNetDev7-Jan-10 15:36 
GeneralRe: subclassing a treeviewitem Pin
hb521342148-Jan-10 7:15
hb521342148-Jan-10 7:15 
GeneralRe: subclassing a treeviewitem Pin
hb521342148-Jan-10 7:46
hb521342148-Jan-10 7:46 
QuestionZoomIn and zoomout Pin
Member 47046755-Jan-10 18:36
Member 47046755-Jan-10 18:36 
AnswerRe: ZoomIn and zoomout Pin
Mark Salsbery6-Jan-10 8:49
Mark Salsbery6-Jan-10 8:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.