MoveFocus skips controls - c#

I want to allow users to navigate within a control using the Arrow Keys.
Users should still be able to navigate horizontally with the Tab and Shift + Tab controls but I want them to be able to navigate vertically (which may skip controls that would be focused if they navigated horizontally).
If I use the MoveFocus method on UIElement what seems to happen is that certain controls are skipped such as buttons and editable combo-boxes.
Does anyone know why this is? These controls are focused normally using TAB but FocusDirections of Up/Down/Next seem to skip the controls. If I take a look at PredictFocus it seems to report that Buttons should be focusable in this way but not editable combo-boxes.
Demo code below:
XAML:
<Window x:Class="Focus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<Style x:Key="FocusStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Stroke="CadetBlue" StrokeThickness="2" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox1" Width="75"/>
<TextBox Width="200"
Name="txtBox1"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="button1" Width="75" />
<Button Width="200"
Name="button1"
Height="25"
Margin="5"
Content="Hello"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox2" Width="75"/>
<TextBox Width="200"
Name="text2"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="comboBox" Width="75"/>
<ComboBox Width="200"
Margin="5"
Name="comboBox"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox4" Width="75"/>
<TextBox Width="200"
Margin="5"
Name="txtBox4"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="comboBox2" Width="75"/>
<ComboBox Width="200"
Margin="5"
IsEditable="True"
Name="comboBox2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<TextBlock Margin="3" Text="{Binding FocussedControl.Name, StringFormat=Focused Control: {0}}" />
<TextBlock Margin="3" Text="{Binding PredictedFocusControl.Name, StringFormat=Predicted Focus {0}}"/>
</StackPanel>
</Grid>
</Window>
MainWindow.Xaml.cs :
using System.Windows;
using System.Windows.Input;
namespace Focus
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public IInputElement FocussedControl
{
get { return (IInputElement)GetValue(FocussedControlProperty); }
set { SetValue(FocussedControlProperty, value); }
}
public static readonly DependencyProperty FocussedControlProperty =
DependencyProperty.Register("FocussedControl", typeof(IInputElement), typeof(MainWindow));
public DependencyObject PredictedFocusControl
{
get { return (DependencyObject)GetValue(PredictedFocusControlProperty); }
set { SetValue(PredictedFocusControlProperty, value); }
}
public static readonly DependencyProperty PredictedFocusControlProperty =
DependencyProperty.Register("PredictedFocusControl", typeof(DependencyObject), typeof(MainWindow));
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
FocussedControl = e.NewFocus;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.Down)
{
var success = (FocussedControl as UIElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
System.Diagnostics.Debug.Assert(success);
PredictedFocusControl = (FocussedControl as UIElement).PredictFocus(FocusNavigationDirection.Down);
}
else if (e.Key == Key.Up)
{
var success = (FocussedControl as UIElement).MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
PredictedFocusControl = (FocussedControl as UIElement).PredictFocus(FocusNavigationDirection.Up);
}
}
}
}

I solved the problem by palying with FocusManager.IsFocusScope="True" Focusable="True" now it works:
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox1" Width="75"/>
<TextBox Width="200"
Name="txtBox1"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Focusable="True" FocusManager.IsFocusScope="True">
<Label Content="button1" Width="75" />
<Button Width="200" Focusable="True" IsTabStop="True"
Name="button1" FocusManager.IsFocusScope="True"
Height="25"
Margin="5"
Content="Hello"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox2" Width="75"/>
<TextBox Width="200"
Name="text2"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="comboBox" Width="75"/>
<ComboBox Width="200" Focusable="True"
Margin="5" IsTabStop="True"
Name="comboBox"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="txtBox4" Width="75"/>
<TextBox Width="200"
Margin="5"
Name="txtBox4"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal" FocusManager.IsFocusScope="True" Focusable="True">
<Label Content="comboBox2" Width="75"/>
<ComboBox Width="200" Focusable="True"
Margin="5" IsTabStop="True"
IsEditable="True"
Name="comboBox2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<TextBlock Margin="3" Text="{Binding FocussedControl.Name, StringFormat=Focused Control: {0}}" />
<TextBlock Margin="3" Text="{Binding PredictedFocusControl.Name, StringFormat=Predicted Focus {0}}"/>
</StackPanel>
</Grid>
Can't tell you why...

I fixed it by setting IsTabStop=true in the ComboBox even though Tab was working to focus on the control:
<Window x:Class="Focus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<Style x:Key="FocusStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Stroke="CadetBlue" StrokeThickness="2" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="75" Content="txtBox1" />
<TextBox Name="txtBox1"
Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
<TextBox Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="75" Content="button1" />
<Button Name="button1"
Width="200"
Height="25"
Margin="5"
HorizontalAlignment="Left"
FocusVisualStyle="{StaticResource FocusStyle}"
VerticalAlignment="Top"
Content="Hello" />
<TextBox Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="75" Content="txtBox2" />
<TextBox Name="text2"
Width="200"
Margin="5"
HorizontalAlignment="Left"
FocusVisualStyle="{StaticResource FocusStyle}"
VerticalAlignment="Top" />
<TextBox Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="75" Content="comboBox" />
<ComboBox Name="comboBox"
Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}">
<ComboBoxItem Content="Item1" />
<ComboBoxItem Content="Item2" />
<ComboBoxItem Content="Item3" />
<ComboBoxItem Content="Item4" />
</ComboBox>
<TextBox Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="75" Content="txtBox4" />
<TextBox Name="txtBox4"
Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
<TextBox Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<Label Width="75" Content="comboBox2" />
<ComboBox Name="comboBox2"
Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsTabStop="True"
FocusVisualStyle="{StaticResource FocusStyle}"
IsEditable="True" />
<TextBox Width="200"
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FocusVisualStyle="{StaticResource FocusStyle}" />
</StackPanel>
<TextBlock Margin="3" Text="{Binding FocussedControl.Name, StringFormat=Focused Control: {0}}" />
<TextBlock Margin="3" Text="{Binding PredictedFocusControl.Name, StringFormat=Predicted Focus {0}}" />
</StackPanel>
</Grid>
</Window>

Related

Combobox items not displaying from LINQ query

I am trying to populate a combo-box with the contents from a table
(`InspectionTypes`) from a LINQ query without success for weeks.
When I insert a breakpoint, I can see that the data is there and the count
is correct, but nothing shows up in the drop-down list. I have a desktop
application with WPF and EF6. I am currently only working with
inspectionType10ComboBox but, there are nine other comboboxes that will
function exactly the same and use the exact same data. I am attempting to
build a quote based on which inspection(s) have been requested.
It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.
**My code behind is as follows:**
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Entity;
using System.Data.SQLite.Linq;
using System.Linq;
using System.Linq.Expressions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for EstimatesScreen.xaml
/// </summary>
public partial class EstimatesScreen : Window
{
MIDatabase01Entities1 context = new MIDatabase01Entities1();
CollectionViewSource estimatesViewSource;
public EstimatesScreen()
{
InitializeComponent();
estimatesViewSource = ((CollectionViewSource)
(FindResource("estimatesViewSource")));
DataContext = this;
FillComboBoxes();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource estimatesViewSource =
((System.Windows.Data.CollectionViewSource)
(this.FindResource("estimatesViewSource")));
context.Estimates.Load();
estimatesViewSource.Source = context.Estimates.Local;
}
private void FillComboBoxes()
{
var result = context.InspectionTypes.Select(p => new
{
ID = p.InspectionTypesID,
Name = p.InspectionTypeName
}).ToList();
inspectionType10ComboBox.ItemsSource = result;
inspectionType10ComboBox.SelectedValuePath = "ID";
inspectionType10ComboBox.DisplayMemberPath = "Name";
}
}
}
**My XAML is as follows (edited to reduce size. Removed "Newgrid which is
a duplacate if ExistingGrid with "New" prefix for all fields.):**
<Window x:Class="WpfApp1.EstimatesScreen"
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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Monroe Inspections LLC" Height="600" Width="1000"
Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="estimatesViewSource" d:DesignSource="
{d:DesignInstance {x:Type local:Estimate}, CreateList=True}"/>
<CollectionViewSource x:Key="inspectionTypesViewSource"
d:DesignSource="{d:DesignInstance {x:Type local:InspectionType},
CreateList=True}"/>
<RoutedUICommand x:Key="FirstCommand" Text="First"/>
<RoutedUICommand x:Key="LastCommand" Text="Last"/>
<RoutedUICommand x:Key="NextCommand" Text="Next"/>
<RoutedUICommand x:Key="DeleteCommand" Text="Delete"/>
<RoutedUICommand x:Key="UpdateCommand" Text="Update"/>
<RoutedUICommand x:Key="AddCommand" Text="Add"/>
<RoutedUICommand x:Key="CancelCommand" Text="Cancel"/>
<RoutedUICommand x:Key="PreviousCommand" Text="Previous"/>
<Style x:Key="NavButton" TargetType="{x:Type Button}" BasedOn="
{x:Null}">
<Setter Property="FontSize" Value="24"/>
<Setter Property="FontFamily" Value="Segoe UI Symbol"/>
<Setter Property="Margin" Value="2,2,2,0"/>
<Setter Property="Width" Value="40"/>
<Setter Property="Height" Value="auto"/>
</Style>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource FirstCommand}"
Executed="FirstCommandHandler"/>
<CommandBinding Command="{StaticResource LastCommand}"
Executed="LastCommandHandler"/>
<CommandBinding Command="{StaticResource NextCommand}"
Executed="NextCommandHandler"/>
<CommandBinding Command="{StaticResource PreviousCommand}"
Executed="PreviousCommandHandler"/>
<CommandBinding Command="{StaticResource DeleteCommand}"
Executed="DeleteCommandHandler"/>
<CommandBinding Command="{StaticResource UpdateCommand}"
Executed="UpdateCommandHandler"/>
<CommandBinding Command="{StaticResource AddCommand}"
Executed="AddCommandHandler"/>
<CommandBinding Command="{StaticResource CancelCommand}"
Executed="CancelCommandHandler"/>
</Window.CommandBindings>
<Grid Margin="0,0,0,0">
<Label Content="QUOTES" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="224" Height="51" FontWeight="Bold"
FontSize="36" Margin="173,9,0,0"/>
<Button Content="Return" HorizontalAlignment="Left" Margin="10,66,0,0"
VerticalAlignment="Top" Height="41" Width="136" Click="Return_Click"
FontWeight="Bold" FontFamily="Arial"/>
<Grid x:Name="ComboboxGrid" DataContext="{StaticResource
estimatesViewSource}" HorizontalAlignment="Left" Margin="165,62,0,0"
VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Existing Quote Customer:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/>
<ComboBox x:Name="customerComboBox" Grid.Column="1"
DisplayMemberPath="Customer" HorizontalAlignment="Left" Height="Auto"
ItemsSource="{Binding}" Margin="3" Grid.Row="0" VerticalAlignment="Center"
Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</Grid>
<Grid x:Name="ExistingGrid" DataContext="{StaticResource
estimatesViewSource}" HorizontalAlignment="Left" Margin="176,112,0,0"
VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="115"/>
<ColumnDefinition Width="230"/>
<ColumnDefinition Width="210"/>
<ColumnDefinition Width="82"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Label Content="Customer:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="1"
VerticalAlignment="Center"/>
<TextBox x:Name="customerTextBox" Grid.Column="1"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="1" Text="
{Binding Customer, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="200"/>
<Label Content="Discount Price:" Grid.Column="2"
HorizontalAlignment="Right" Margin="3" Grid.Row="11"
VerticalAlignment="Center"/>
<TextBox x:Name="discountPriceTextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="11" Text="
{Binding DiscountPrice, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<Label Content="Expiration Date:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="11"
VerticalAlignment="Center"/>
<DatePicker x:Name="estExpirationDateDatePicker" Grid.Column="1"
HorizontalAlignment="Left" Margin="3" Grid.Row="11" SelectedDate="{Binding
EstExpirationDate, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="125"/>
<Label Content="Quote Accepted:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="12"
VerticalAlignment="Center"/>
<CheckBox x:Name="estimateAcceptedCheckBox" Content=""
Grid.Column="1" HorizontalAlignment="Left" IsChecked="{Binding
EstimateAccepted, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" Margin="3" Grid.Row="12"
VerticalAlignment="Center"/>
<Label Content="Quote Date:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="10"
VerticalAlignment="Center"/>
<DatePicker x:Name="estimateDateDatePicker" Grid.Column="1"
HorizontalAlignment="Left" Margin="3" Grid.Row="10" SelectedDate="{Binding
EstimateDate, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="125"/>
<Label Content="Estimated Price Total:" Grid.Column="2"
HorizontalAlignment="Right" Margin="3" Grid.Row="12"
VerticalAlignment="Center"/>
<TextBox x:Name="estimatedPriceTotalTextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="12" Text="
{Binding EstimatedPriceTotal, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<Label Content="Quote ID:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="0"
VerticalAlignment="Center"/>
<TextBox x:Name="estimatesIDTextBox" Grid.Column="1"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="0" Text="
{Binding EstimatesID, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<Label Content="Square Footage:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="5"
VerticalAlignment="Center"/>
<TextBox x:Name="squareFootageTextBox" Grid.Column="1"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="5" Text="
{Binding SquareFootage, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost1TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="1" Text="
{Binding InspectionCost1, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost10TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="10" Text="
{Binding InspectionCost10, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost2TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="2" Text="
{Binding InspectionCost2, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost3TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="3" Text="
{Binding InspectionCost3, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost4TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="4" Text="
{Binding InspectionCost4, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost5TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="22" Margin="3" Grid.Row="5" Text="
{Binding InspectionCost5, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost6TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="22" Margin="3" Grid.Row="6" Text="
{Binding InspectionCost6, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost7TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="22" Margin="3" Grid.Row="7" Text="
{Binding InspectionCost7, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost8TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="8" Text="
{Binding InspectionCost8, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<TextBox x:Name="inspectionCost9TextBox" Grid.Column="3"
HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="9" Text="
{Binding InspectionCost9, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<ComboBox x:Name="inspectionType1ComboBox"
Grid.Column="2" Grid.Row="1"
Height="24" Width="200" Margin="3"
HorizontalAlignment="Left" VerticalAlignment="Center"
DataContext="inspectionTypesViewSource"
SelectedValuePath="InspectionTypeName"
SelectedValue="InspectionTypeName"
DisplayMemberPath="InspectionTypeName"
ItemsSource="{Binding result}" >
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
This is the ComboBox I am currently working on. There are nine others that will have to work exactly the same, with the dame data.
<ComboBox x:Name="inspectionType10ComboBox"
Grid.Column="2" Grid.Row="10"
Height="24" Width="200" Margin="3"
HorizontalAlignment="Left" VerticalAlignment="Center">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType2ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType2" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="2"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType3ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType3" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="3"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType4ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType4" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="4"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType5ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType5" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="5"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType6ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType6" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="6"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType7ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType7" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="7"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType8ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType8" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="8"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<ComboBox x:Name="inspectionType9ComboBox" Grid.Column="2"
DisplayMemberPath="InspectionType9" HorizontalAlignment="Left"
Height="Auto" ItemsSource="{Binding}" Margin="3" Grid.Row="9"
VerticalAlignment="Center" Width="200">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<Label Content="Address:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="4"
VerticalAlignment="Center"/>
<TextBox x:Name="addressTextBox" Grid.Column="1"
HorizontalAlignment="Left" Height="24" Margin="3" Grid.Row="4" Text="
{Binding Address, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="200"/>
<Label Content="Inspection Type" Grid.Column="2"
HorizontalAlignment="Left" Margin="3" VerticalAlignment="Top" Width="97"/>
<Label Content="Cost" Grid.Column="3" HorizontalAlignment="Left"
Margin="3" VerticalAlignment="Top" Width="47"/>
<Label Content="Email:" Grid.Column="0" HorizontalAlignment="Left"
Margin="3" Grid.Row="2" VerticalAlignment="Center"/>
<TextBox x:Name="customerEmailTextBox" HorizontalAlignment="Left"
Height="23" Margin="3" Grid.Row="2" Text="{Binding CustomerEmail,
Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"
VerticalAlignment="Center" Width="215" Grid.Column="1"/>
<Label Content="Crawl Space:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="6"
VerticalAlignment="Center"/>
<CheckBox x:Name="crawalSpaceCheckBox1" Content=""
HorizontalAlignment="Left" IsChecked="{Binding CrawalSpace, Mode=TwoWay,
NotifyOnValidationError=true, ValidatesOnExceptions=true}" Margin="3"
Grid.Row="6" VerticalAlignment="Center" Grid.Column="1"/>
<TextBox x:Name="additionalDiscountTextBox" Grid.Column="1"
HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="8" Text="
{Binding AdditionalDiscount, Mode=TwoWay, NotifyOnValidationError=true,
ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="70"/>
<Label Content="Extra Discount:" Grid.Column="0"
HorizontalAlignment="Left" Margin="3" Grid.Row="8"
VerticalAlignment="Center"/>
</Grid>
`NewGrid (removed for length constraints) goes here.`
<StackPanel Orientation="Horizontal" Height="35"
VerticalAlignment="Bottom" Background="Gainsboro" DataContext="
{StaticResource estimatesViewSource}" Margin="0,0,0,0">
<Button Name="btnFirst" Content="|◄" Command="{StaticResource
FirstCommand}" Style="{StaticResource NavButton}"/>
<Button Name="btnPrev" Content="◄" Command="{StaticResource
PreviousCommand}" Style="{StaticResource NavButton}"/>
<Button Name="btnNext" Content="►" Command="{StaticResource
NextCommand}" Style="{StaticResource NavButton}"/>
<Button Name="btnLast" Content="►|" Command="{StaticResource
LastCommand}" Style="{StaticResource NavButton}"/>
<Button Name="btnDelete" Content="Delete" Command="{StaticResource
DeleteCommand}" FontSize="11" Width="80" Style="{StaticResource
NavButton}"/>
<Button Name="btnAdd" Content="Add New" Command="{StaticResource
AddCommand}" FontSize="11" Width="80" Style="{StaticResource NavButton}"/>
<Button Name="btnUpdate" Content="Save" Command="{StaticResource
UpdateCommand}" FontSize="11" Width="80" Style="{StaticResource
NavButton}"/>
<Button Name="btnCancel" Content="Cancel" Command="{StaticResource
CancelCommand}" FontSize="11" Width="80" Style="{StaticResource
NavButton}"/>
</StackPanel>
</Grid>
</Window>`
`I have been looking at hundreds of samples and I am lost. The data for
the ComboBox is in my InspectionTypes table.
`

C# WPF listBox vertical alignment

I have a listBox with a checkbox I want to manage the alignment through setting the vertical alignment.
I have seen several solution here e.g. set verticalContentAlignment or verticalAlignment to top but that didn't work.
I've also seen some setting a style but couldn't manage to make it done.
What I'd like is to know WHY the ONLY element is down there. I can't understand why it's stuck in the middle.
Here's my code:
<ListBox x:Name="lb2Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.8,0" >
<CheckBox x:Name="cbEasyRunManagesPcDmis" Margin="20" Content="EasyRun Manages PC-DMIS" HorizontalAlignment="Left" VerticalAlignment="Top" Click="cbTouchScreen_Click" />
</ListBox>
thanx for any help
Patrick
--- EDIT ---
As requested by Frisbee here is the complete xaml. Sorry for its length:
<Base:WindowViewBase x:Class="EasyRun.Views.MainView.MainView"
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:Base="clr-namespace:Cannoli.Base;assembly=Cannoli"
xmlns:design="clr-namespace:EasyRun.Views.MainView.Design"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:CannoliBorderlessWindow="clr-namespace:CannoliBorderlessWindow;assembly=Cannoli.BorderlessWindow"
d:DesignWidth="212.32" d:DesignHeight="109.92"
Title="EASY RUN 2.0" Height="600" Width="900" MinWidth="500" FontFamily="/EasyRun2.0;component/Resources/Fonts/#FontAwesome" Icon="/EasyRun2.0;component/Resources/Images/Lightning.png"
>
<d:WindowViewBase.DataContext>
<design:DesignMainViewModel />
</d:WindowViewBase.DataContext>
<i:Interaction.Behaviors>
<CannoliBorderlessWindow:CannoliBorderlessWindowBehavior/>
</i:Interaction.Behaviors>
<!-- RISORSE GLOBALI: VARIABILI -->
<Window.Resources>
<System:Double x:Key="BUTTON_HEIGHT">50</System:Double>
<System:Double x:Key="BUTTON_FONTSIZE">20</System:Double>
</Window.Resources>
<Grid x:Name="MainGrid">
<Grid.Background>
<ImageBrush ImageSource="/EasyRun2.0;component/Resources/Images/gradientWallpaper.jpg"/>
</Grid.Background>
<TextBlock Text="{Binding WelcomeMessage}" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" Margin="428,128,99.6,228"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Bottom" Margin="0,0,0,20">
</StackPanel>
<TabControl TabStripPlacement="Left" Margin="0,0,-0.4,0" Background="{x:Null}">
<!-- +++++++++++++ TAB1 ++++++++++++ -->
<TabItem Name="tabItem1" HorizontalAlignment="Center" Height="80" FontSize="50" Background="{x:Null}" VerticalAlignment="Top" >
<TabItem.Header>
<StackPanel>
<TextBlock HorizontalAlignment="Center" Text=""/>
<TextBlock Name="tbTab1" HorizontalAlignment="Center" Visibility="Hidden" FontSize="20"/>
</StackPanel>
</TabItem.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="Border1Tab1" BorderBrush="Gainsboro" BorderThickness="5" Width="200" CornerRadius="8,8,3,3" Margin="10,10,10,10.4" >
<ListBox x:Name="lbButtons1" Background="{x:Null}" BorderBrush="{x:Null}" Margin="10,10,10,10" />
</Border>
<Border x:Name="Border2Tab1" BorderBrush="Gainsboro" MinWidth="100" BorderThickness="5" CornerRadius="8,8,3,3" Grid.Column="1" Margin="10,10,9.2,10" />
</Grid>
</TabItem>
<!-- +++++++++++++ TAB2 ++++++++++++ -->
<TabItem Name="tabItem2" HorizontalAlignment="Center" Height="80" FontSize="50">
<TabItem.Header>
<StackPanel>
<TextBlock Text=""/>
<TextBlock Name="tbTab2" Visibility="Hidden" FontSize="20"/>
</StackPanel>
</TabItem.Header>
<TabItem.Background>
<ImageBrush/>
</TabItem.Background>
</TabItem>
<!-- +++++++++++++ TAB3 ++++++++++++ -->
<TabItem Name="tabItem3" HorizontalAlignment="Center" Height="80" FontSize="50" Background="{x:Null}" >
<TabItem.Header>
<StackPanel>
<TextBlock Text="" HorizontalAlignment="Center" />
<TextBlock Name="tbTab3" Visibility="Hidden" FontSize="20"/>
</StackPanel>
</TabItem.Header>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="Border1Tab3" BorderBrush="Gainsboro" BorderThickness="5" Width="200" CornerRadius="8,8,3,3" Margin="10,10,10,10" >
<ListBox x:Name="lbButtons3" FontSize="{StaticResource BUTTON_FONTSIZE}" HorizontalContentAlignment="Stretch" Background="{x:Null}" BorderBrush="{x:Null}" >
<Button Name="bt1Tab3" Background="{x:Null}" Content="1" Click="Button_Click" />
<Button Name="bt2Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="2" Click="Button_Click"/>
<Button Name="bt3Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="3" Click="Button_Click"/>
<Button Name="bt4Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="4" Click="Button_Click"/>
<Button Name="bt5Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="5" Click="Button_Click"/>
<Button Name="bt6Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="6" Click="Button_Click"/>
<Button Name="bt7Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="7" Click="Button_Click"/>
<Button Name="bt8Tab3" Background="{x:Null}" Height="{StaticResource BUTTON_HEIGHT}" Content="8" Click="Button_Click"/>
</ListBox>
</Border>
<Border x:Name="Border2Tab3" BorderBrush="Gainsboro" MinWidth="100" BorderThickness="5" CornerRadius="8,8,3,3" Grid.Column="1" Margin="10,10,10,10" >
<StackPanel >
<ListBox x:Name="lb1Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" d:IsHidden="True" >
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="lbLanguage" Margin="20" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" Grid.Row="1"><Run Text="Language"/></TextBlock>
<ComboBox x:Name="cmbLanguages" Margin="20" HorizontalAlignment="Left" VerticalAlignment="Center" Width="246" Height="35" DropDownClosed="cmbLanguages_DropDownClosed"/>
</StackPanel>
<CheckBox x:Name="cbTouchScreen" Margin="20" Content="Use touch screen" HorizontalAlignment="Left" VerticalAlignment="Top" Click="cbTouchScreen_Click" />
<CheckBox x:Name="cbEnableImages" Margin="20" Content="Enable Images" HorizontalAlignment="Left" VerticalAlignment="Top" />
<StackPanel Name="spImageDimension" Orientation="Horizontal">
<TextBlock x:Name="lbImageDimension" Margin="20" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center"><Run Text="Image dimension"/></TextBlock>
<ComboBox x:Name="cmbImageDimension" Margin="20" Text="250 px" HorizontalAlignment="Left" DropDownClosed="cmbImageDimension_DropDownClosed" VerticalAlignment="Center" Width="246" Height="35" IsEditable="True">
<TextBlock ><Run Text="50 px"/></TextBlock>
<TextBlock ><Run Text="100 px"/></TextBlock>
<TextBlock ><Run Text="150 px"/></TextBlock>
<TextBlock ><Run Text="200 px"/></TextBlock>
<TextBlock ><Run Text="250 px"/></TextBlock>
</ComboBox>
</StackPanel>
</ListBox>
<ListBox x:Name="lb2Tab3" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Top" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.8,0" >
<CheckBox x:Name="cbEasyRunManagesPcDmis" Margin="20" Content="EasyRun Manages PC-DMIS" HorizontalAlignment="Left" VerticalAlignment="Top" Click="cbTouchScreen_Click" />
</ListBox>
<ListBox x:Name="lb3Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" />
<ListBox x:Name="lb4Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" d:IsHidden="True" >
</ListBox>
<ListBox x:Name="lb5Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" d:IsHidden="True" >
</ListBox>
<ListBox x:Name="lb6Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" d:IsHidden="True" >
</ListBox>
<ListBox x:Name="lb7Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" d:IsHidden="True" >
</ListBox>
<ListBox x:Name="lb8Tab3" HorizontalContentAlignment="Stretch" FontSize="{StaticResource BUTTON_FONTSIZE}" Background="{x:Null}" BorderBrush="{x:Null}" Margin="0,0,0.4,0" d:IsHidden="True" >
</ListBox>
</StackPanel>
</Border>
</Grid>
</TabItem>
</TabControl>
</Grid>
You need to align the ListBox
<ListBox x:Name="lb2Tab3" HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
If that does not work then post more of your XAML
So the problem was related with the fact that there are several listBox one on top of the other. I thought that making them hidden was enough but when I painted the second listBox in black I saw that it was not starting from the top (see image).
So the solution was to set Visibility = Collapsed for all the listbox that are not in use.
Thanks to kirotab for putting me on the right path. Not sure if I can vote your comment.

Inner StackPanel isn't visible

I have one StackPanel inside another with a couple of labels inside the inner StackPanel. For some reason the the inner StackPanel and the Labels are not displaying at all, why is this?
<Window x:Class="WeatherApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Weather Application" Height="550" Width="850" Loaded="Window_Loaded" IsEnabled="True" ResizeMode="CanMinimize" Icon="/WeatherApplication;component/Images/weatherIcon.png">
<Grid Height="522" Background="#FFE7E7E7">
<DockPanel>
<Menu DockPanel.Dock="Top" Height="35" FontWeight="Bold" FontFamily="Arial">
<MenuItem Header="_File" Height="30" VerticalAlignment="Center" Padding="7,8,8,3">
<MenuItem Header="_Settings"/>
<MenuItem Header="_Close"/>
</MenuItem>
<MenuItem Header="_Help" Padding="7,8,8,3">
<MenuItem Header="_Guide"/>
<MenuItem Header="_About"/>
</MenuItem>
<Menu.Background>
<ImageBrush ImageSource="/WeatherApplication;component/Images/menuBG.png" />
</Menu.Background>
</Menu>
<StackPanel Width="230" HorizontalAlignment="Left" Margin=" 5">
<Label Content="Search location" FontFamily="Arial" FontSize="14" Height="28" Name="searchLocationLabel" IsEnabled="False" />
<StackPanel Orientation="Horizontal">
<Label Width="100" FontFamily="Arial" FontSize="14" Margin="3">Town</Label>
<TextBox Margin="3" Width="120"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="100" FontFamily="Arial" FontSize="14" Margin="3">County</Label>
<TextBox Margin="3" Width="120"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="100" FontFamily="Arial" FontSize="14" Margin="3">Post Code</Label>
<TextBox Margin="3" Width="120"></TextBox>
</StackPanel>
<Button Content="Search" Height="23" Name="searchButton" Width="75" HorizontalAlignment="Right" Margin="0 5"/>
<Separator Height="5" Name="separator1" Width="245" HorizontalAlignment="Right"/>
<Label Content="Weather Today" FontFamily="Arial" FontSize="14" Height="28" Name="weatherTodayLabel" IsEnabled="False" Margin="0 5"/>
<Label Content="Bury St Edmunds" FontFamily="Arial" FontSize="14" FontWeight="Bold" Height="28" Name="label1" />
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Temperature:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="75°"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Condition:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Mostly Cloudy"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Hi:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="75°"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Low:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="75°"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Wind Speed:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="12mph"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Humidity:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="75°"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Sunrise:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="11:40am"></Label>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Sunset:"></Label>
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="4:41pm"></Label>
</StackPanel>
</StackPanel>
<StackPanel Width="580" Height="400" HorizontalAlignment="right" Background="#FFD14040" VerticalAlignment="Top" Margin="30 -10 -1 0" ZIndex="-1">
<Border Width="575" Height="400" HorizontalAlignment="right" Background="Transparent" BorderBrush="Black" BorderThickness="1" Margin="0 0 -0 0">
<Border.Effect>
<DropShadowEffect ShadowDepth="1" BlurRadius="10"/>
</Border.Effect>
</Border>
</StackPanel>
<StackPanel Name="stackPanel111" HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">
<StackPanel Height="100" Background="AliceBlue">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Tuesday" />
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="75°" />
</StackPanel>
</StackPanel>
</DockPanel>
</Grid>
</Window>
This is how it looks now:
The Width="580" in your first stack panel conflicts with the Width="525" of the Window. It pushes the content off-screen.
Remove the StackPanel's Width, and you will be fine:
<StackPanel Name="stackPanel111" HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">
<StackPanel Height="100" Background="AliceBlue">
<Label Width="110" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="Tuesday" />
<Label Width="130" FontFamily="Arial" FontSize="14" FontWeight="Bold" Content="75°" />
</StackPanel>
</StackPanel>
Running this in an empty application, this is the result:

The name 'xxx' does not exist in the current context in WP7

this is my xaml code:
<TextBox x:Name="name_box_det" Text="{Binding Name}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
And this is C# code, to get text value:
var name_text_det = name_box_det.Text;
And I'm getting this exception:
The name 'name_box_det' does not exist in the current context
Xaml code is copied from another xaml, but I tried to write completely new and it does not help. Do you know where is error?
This is complete XAML code:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Margin="0" Grid.Row="1" Source="devdesk.png" Stretch="Fill"/>
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,45,12,12"Orientation="Horizontal">
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Height="27" Margin="0,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record index:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox Text="{Binding Index}" x:Name="index_box_det" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Name="record_name" Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record name:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox x:Name="name_box_det" Text="{Binding Name}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Beneficiary:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox x:Name="beneficiary_box_det" Text="{Binding Beneficiary}" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Height="27" Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Price:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
<TextBox x:Name="price_box_det" Height="65" Text="{Binding Price}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Deadline:" Foreground="#FF6C6C6C"/>
<TextBox x:Name="deadline_box_det" Text="{Binding Deadline}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
<TextBlock Margin="0,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Description:" Foreground="#FF6C6C6C" Height="27" VerticalAlignment="Bottom"/>
<TextBox x:Name="description_box_det" Text="{Binding Description}" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667" Height="285" VerticalAlignment="Bottom"/>
<Button Width="375" x:Name="edtbtn" Content="Edit this record" Click="edtbtn_Click" Height="88" Margin="-12,-10,0,0" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" BorderThickness="3" Foreground="#FF40AA2F" BorderBrush="#FF40AA2F" Background="{x:Null}"/>
<Button Width="100" x:Name="dltbtn" Click="dltbtn_Click" Height="88" Margin="-12,-98,0,0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" BorderThickness="3" Foreground="#FF40AA2F" BorderBrush="#FF40AA2F" Background="{x:Null}">
<StackPanel>
<Image Source="delete.png"/>
</StackPanel>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
name_box_det exists within the ItemTemplate of your ListBox. This is a different context to your page and hence the error. Because an instance of this TextBlock will exist for every item in the collection your there's no way to know which one you're referring to in the code behind.
I'm guessing you're doing this in the Delete button click event handler. As you haven't provided a full repro of what you're doing here's an example of how it may be done.
Assuming the UI contains:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" />
<Button Content="delete" Grid.Column="1" Click="DeleteClicked"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the DataContext is something like:
this.DataContext = new[]
{
new SimpleViewModel { Name = "one" },
new SimpleViewModel { Name = "two" },
new SimpleViewModel { Name = "three" },
};
Then the click event handler can look like this:
private void DeleteClicked(object sender, RoutedEventArgs e)
{
MessageBox.Show(
"You're trying to delete " +
((sender as FrameworkElement).DataContext as SimpleViewModel).Name);
}

Listbox No Scrollbar

I have been up and down looking on the internet and many people seem to have this problem but its generally solved by changing the container to a grid, constraining the height etc. etc. I can't to seem to get this to work.
I have an observableCollection thats feeding into a DataTemplate. I can't for th life of me get the scrollbar working. Its there but not enabling. Thanks Scott
<TabItem Header="select a call" x:Name="TabActiveCalls" Style="{DynamicResource MyTabItem}" FontFamily="QuickType">
<Grid Margin="0.125,0.412,3.125,0" Height="471" HorizontalAlignment="Stretch">
<StackPanel Orientation="Horizontal" Width="839.14" HorizontalAlignment="Left" VerticalAlignment="Top" Height="56">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="0,0,10,0" Width="741.14">
<StackPanel Height="28" Orientation="Horizontal" Width="733.14" HorizontalAlignment="Left">
<TextBlock x:Name="txtHistoryFound" TextWrapping="Wrap" Text="*" Foreground="#FFE20A0A" Visibility="Collapsed"/>
<TextBlock TextWrapping="Wrap" Text="filter by:" Margin="5,0,10,0" Foreground="#FF585AD4" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="call no" Margin="5,0,2,0" VerticalAlignment="Center" Foreground="#FF5E88DA"/>
<TextBox Template="{StaticResource TextBoxBaseControlTemplate}" x:Name="searchCallNo" TextChanged="searchCallNo_TextChanged" TextWrapping="Wrap" Width="67" Foreground="#FF1341B1" TextAlignment="Center" Margin="5,0,10,0" Height="22" VerticalAlignment="Center" />
<TextBlock TextWrapping="Wrap" Text="postcode" Margin="5,0,2,0" VerticalAlignment="Center" Foreground="#FF5E88DA"/>
<TextBox Template="{StaticResource TextBoxBaseControlTemplate}" x:Name="searchPostcode" TextWrapping="Wrap" Width="67" Foreground="#FF1341B1" TextAlignment="Center" Margin="5,0,10,0" Height="22" VerticalAlignment="Center"/>
<TextBlock Height="23" x:Name="txtSiteName" FontSize="16" Foreground="#FF0E7C0B" Width="409" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0" TextAlignment="Right" Text="Airedale International Ltd" />
</StackPanel>
<StackPanel Width="733.14" HorizontalAlignment="Left">
<Border Height="21" Margin="5,6,8,0" CornerRadius="3,3,0,0" BorderThickness="1" BorderBrush="#FFC0BABA">
<StackPanel Orientation="Horizontal" Background="#FFD0D5DE">
<TextBlock TextWrapping="Wrap" Text="CALL NO. / DATE DUE/ CUSTOMER" Margin="5,0,0,0" Foreground="{DynamicResource ListTitle}" FontSize="10.667" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="ENGINEER / ADDRESS" Margin="114,0,0,0" Foreground="{DynamicResource ListTitle}" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="REPORT" Margin="43,0,0,0" Foreground="{DynamicResource ListTitle}" RenderTransformOrigin="2.543,0.429" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="CALL" Margin="28,0,0,0" Foreground="{DynamicResource ListTitle}" RenderTransformOrigin="2.543,0.429" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="POSITION" Margin="43,0,0,0" Foreground="{DynamicResource ListTitle}" RenderTransformOrigin="2.543,0.429" VerticalAlignment="Center"/>
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
<Image Height="56" Width="90" Source="/ComfortReportEng;component/Media/Images/comfort_group.png"/>
</StackPanel>
<ListBox ItemTemplate="{StaticResource DataTemplateReportList}" ItemsSource="{Binding Source={StaticResource cvsReportList}}"
Margin="5,56,8,0" MaxHeight="415" Height="415" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True"/>
</Grid>
</TabItem>
many thanks for getting back. Here is my DataTempate
<DataTemplate x:Key="DataTemplateReportList">
<Border Margin="0,2,0,0" BorderThickness="1" BorderBrush="#FFA19C9C" CornerRadius="3,3,0,0" Width="810.52" Height="50" >
<Grid Background="#FF737B89" Height="48" >
<StackPanel Orientation="Vertical" HorizontalAlignment="Left" Width="274" VerticalAlignment="Top" >
<StackPanel Height="23" Orientation="Horizontal" Margin="0">
<TextBlock TextWrapping="Wrap" Text="{Binding CallNo, Mode=TwoWay}" Foreground="#FF7DF51E" Margin="5,0,0,0" FontFamily="Verdana"/>
<TextBlock TextWrapping="Wrap" Text="{Binding DateDue, Mode=TwoWay, StringFormat=d}" Foreground="White" Margin="5,0,0,0" FontFamily="Verdana"/>
</StackPanel>
<StackPanel Height="23" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Text="{Binding CompanyName, Mode=TwoWay}" Foreground="#FFFFEA00" Margin="5,0,0,0" FontFamily="Verdana"/>
</StackPanel>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Width="456" Orientation="Vertical" Height="46" Margin="274,1,0,1" VerticalAlignment="Top">
<StackPanel Height="23" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Text="{Binding EngineerName, Mode=TwoWay}" Foreground="#FFCAE5C6" Margin="5,0,0,0" FontFamily="Verdana" Width="140"/>
<TextBlock TextWrapping="Wrap" Text="{Binding ReportStatus, Mode=TwoWay}" Foreground="#FF7DF51E" Margin="20,0,0,0" FontFamily="Verdana" Width="50"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CallStatus, Mode=TwoWay}" Foreground="#FF7DF51E" Margin="20,0,0,0" FontFamily="Verdana" Width="50"/>
<TextBlock TextWrapping="Wrap" Text="{Binding Position, Mode=TwoWay}" Foreground="White" Margin="20,0,0,0" FontFamily="Verdana" Width="50"/>
</StackPanel>
<StackPanel Height="23" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Text="{Binding Address, Mode=TwoWay}" Foreground="#FFFFEA00" Margin="5,0,0,0" FontFamily="Verdana" Width="483.12"/>
</StackPanel>
</StackPanel>
<Grid Width="56" HorizontalAlignment="Right" Margin="0,0,12.52,0" VerticalAlignment="Top">
<Image Name="imgInfo" Source="/ComfortReportEng;component/Media/Images/Info4.png" Margin="24,8,0,0" Cursor="Hand" MouseLeftButtonDown="imgInfo_MouseLeftButtonDown"/>
</Grid>
</Grid>
</Border>
</DataTemplate>
Must be a bug. I have the following only..
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ComfortReportEng.Views.EngineerReport.EngineerReport"
Title="Engineer Report" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" SizeToContent="WidthAndHeight" >
<Window.Resources>
<CollectionViewSource x:Key="cvsReportList"/>
</Window.Resources>
<Grid Margin="0.125,0.412,3.125,0" Height="471">
<ListBox Margin="23,64,3,0" Width="834.14" ItemsSource="{Binding Source={StaticResource cvsReportList}}"
x:Name="lbReportList" SelectionChanged="lbReportList_SelectionChanged" Background="#FFEEEFE4" />
</Grid>
and code is
private void LoadReportList()
{
int number = 0;
List<int> myNumbers = new List<int>();
while (number < 80)
{
myNumbers.Add(number);
number += 1;
}
_cvsReportList = (CollectionViewSource)(this.FindResource("cvsReportList"));
_cvsReportList.Source = myNumbers;
}
This was copied over from another project. Completely confused. Please don't worry. It's not a logical problem. Please take this as a close call with no solution. Cheers again Scott
Just to give this completeness. I am not sure why this happens but here is it.
if (System.Environment.MachineName == "SCOTT-PC")
{
this.txtUserName.Text = "Scott Fisher";
this.txtPassword.Password = "palace";
//LoginOK();
}
else
Keyboard.Focus(this.txtUserName);
If I clear the comments on LoginOK procedure I will get no scrollbar. With it commented and I interact with the login screen then everything is fine. Finally here is the code for the LoginOK.
if (LoginService.CheckLogin(txtUserName.Text, txtPassword.Password.ToString()))
{
Helpers.User.ThisUser = this.txtUserName.Text;
EngineerReport.EngineerReport engReport = new EngineerReport.EngineerReport()
{
Owner = Window.GetWindow(this),
WindowStartupLocation =
System.Windows.WindowStartupLocation.
CenterOwner
};
this.Hide();
engReport.ShowDialog();
}
else
{
this.txtPassword.Password = "";
this.txtPassword.Focus();
}
Can you provide you DataTemplate as I believe I have just managed to mock up what you are trying to achieve and I have scrollbars visible and working form the start.
Here is the xaml that I used for the DataTemplate for the ListBox:
<ListBox DataContext="{StaticResource MusicData}" ItemsSource="{Binding XPath=Album}">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<StackPanel Orientation="Horizontal" >
<TextBlock>No.</TextBlock>
<TextBlock Text="{Binding XPath=No}"></TextBlock>
<TextBlock>Title</TextBlock>
<TextBlock Text="{Binding XPath=Title}"></TextBlock>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Hope this helps.
I also found a piece of code I used a while back and this might help. You can try setting the ItemsPanelTemplate:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel>
<ScrollViewer VerticalScrollBarVisibility="Visible" />
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
This will work with any items control as stated in this article:
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx
One final thing I can suggest is to create your own ControlTemplate for the ListBox Like so:
<ListBox.Template>
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>

Categories

Resources