I have a simple WPF dialog that shows any errors that occurred in the program. The view model it's bound to is very simple, just a text property and a list property that displays the list of errors. However, when I show this dialog in my application it shows up as empty, it's just the title bar and a completely white window. As soon as I resize the window all the content populates properly. Why would this be? Here is the constructor of the window:
public Wnd_Errors(string description, List<GamutError> ers)
{
InitializeComponent();
_myVM = new ErrorWindowVM()
{
DescriptionText = description,
Errors = ers,
};
DataContext = _myVM;
}
Here is the entire XAML of the window:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="SPTR.Gamut.Wnd_Errors"
mc:Ignorable="d"
Title="Gamut | Errors"
WindowStartupLocation="CenterScreen"
Icon="Images/Icon_Gamut.ico"
Height="442" Width="846">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SPECtrumStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Background="{StaticResource br_RibbonBackdrop}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="Images/Icon_Error.png" Margin="10" />
<TextBlock x:Name="tb_Desc" Grid.Column="1" FontWeight="Bold" Margin="5" Text="{Binding DescriptionText}"
TextWrapping="Wrap" />
</Grid>
<telerik:RadGridView Grid.Row="1" AutoGenerateColumns="False" RowIndicatorVisibility="Collapsed" GridLinesVisibility="Horizontal"
CanUserFreezeColumns="False" EditTriggers="None" CanUserDeleteRows="False" CanUserInsertRows="False"
ItemsSource="{Binding Errors}" CanUserSelect="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Error Type" DataMemberBinding="{Binding ErrorTypeText}" />
<telerik:GridViewDataColumn Header="Message" DataMemberBinding="{Binding UIText}"
TextWrapping="Wrap" Width="*" />
<telerik:GridViewDataColumn Header="Internal Error" DataMemberBinding="{Binding ErrorTypeText}">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<ScrollViewer MaxWidth="800" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
<TextBlock Text="{Binding InternalMessage, Mode=OneWay}" />
</ScrollViewer>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="Stack Trace" DataMemberBinding="{Binding ErrorTypeText}"
IsVisible="{Binding ShowStackTrace}">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<ScrollViewer MaxHeight="250" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
<TextBox Text="{Binding Stack, Mode=OneWay}" />
</ScrollViewer>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
<CheckBox Grid.Row="2" Content="Show Stack Trace (for error reporting)"
IsChecked="{Binding ShowStackTrace, Mode=TwoWay}" Margin="5,3" />
<Grid Grid.Row="3" Background="{StaticResource br_BaseBar}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48*"/>
<ColumnDefinition Width="371*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,10,0" Grid.Column="1">
<Button Margin="5" Content="OK" HorizontalAlignment="Right" Width="75" IsDefault="True"
x:Name="btn_OK" Click="btn_OK_Click" />
<Button Margin="5" Content="Cancel" HorizontalAlignment="Right" Width="75" IsCancel="True"
x:Name="btn_Cancel" Click="btn_Cancel_Click" />
</StackPanel>
<Button Content="Export to .xlsx" Width="100" Margin="5" HorizontalAlignment="Left"
x:Name="Button2" Click="Button2_Click" Grid.ColumnSpan="2" />
</Grid>
<Separator Grid.Row="2" VerticalAlignment="Bottom" Margin="-1" Background="{StaticResource Br_Orange}" />
</Grid>
</Window>
As you can see I don't even subscribe to any of the window events to have any type of custom handler that could be suppressing anything...
Where do I even start trying to debug this? This is the only window in the application that does this (at least the only one I've encountered it with).
Related
In my WinRT project I have a ListView as follows;
<ListView Grid.Row="1"
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="None"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Background="White"
ItemTemplateSelector="{StaticResource ResourceKey=QuestionDisplay}"
ItemContainerStyle=
"{StaticResource ResourceKey=QuestionListViewItemContainerStyle}">
</ListView>
Note the line;
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation}"
The collection QuestionsAndNavigation ends with some navigation buttons with this in the UserControl;
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding Path=NavigateToPreviousSectionCommand}"><< Save and Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding Path=NavigateToNextSectionCommand}">Save and Next Section >></Button>
</StackPanel>
and displays as follows;
Now the problem is that in the bind path: Survey.SelectedSection.QuestionsAndNavigation I want the command to be located in the great GrandParent class SurveyPageViewModel (which contains the Survey object) rather than in the embedded collection class QuestionsAndNavigation. By default the Data Context looks in the wrong class.
Important to recognise that this is not inheritance. The "Survey" here maps to the SurveyPageViewModel class which contains a Survey that maps to a SurveyViewModel class, a SelectedSection which maps onto a SectionViewModel class and that contains QuestionsAndNavigation which is a collection of QuestionViewModel classes.
So the code in the QuestionViewModel:
private void NavigateToNextSection()
{
NavigateToSection(this.NextSectionId);
}
private void NavigateToPreviousSection()
{
NavigateToSection(this.PreviousSectionId);
}
private void NavigateToSection(int sectionId)
{
NavService.Navigate("NewSection", sectionId);
}
does not go to the SurveyPageViewModel and I cannot set up NavService in the QuestionViewModel class as it is not there, it is a property in the SurveyPageViewModel instead.
EDIT.
So thanks to the answer by Eric, my ListView contains a x:Name = "ListResponses". I use this in the Path of my Buttons:
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenPreviousSectionCommand,
ElementName=ListResponses}"><< Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenNextSectionCommand,
ElementName=ListResponses}">Next Section >></Button>
</StackPanel>
And I have put the event hookups in the SurveyPageViewModel, but they are not wired up for some reason that I would like to know the answer to;
public ICommand SaveThenPreviousSectionCommand { get; private set; }
public ICommand SaveThenNextSectionCommand { get; private set; }
...
(in the constructor)
SaveThenPreviousSectionCommand = new DelegateCommand(DoSaveThenPreviousSection);
SaveThenNextSectionCommand = new DelegateCommand(DoSaveThenNextSection);
...
private async void DoSaveThenPreviousSection()
{
var i = 1;
}
private async void DoSaveThenNextSection()
{
var i = 0;
}
EDIT: This is the full XAML file for the page
<prism:VisualStateAwarePage x:Class="M.Survey.Views.SurveyPage"
x:Name="pageRoot"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="using:M.Survey.Views"
xmlns:local="using:M.Survey"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
xmlns:mvvm="using:Microsoft.Practices.Prism.Mvvm"
mvvm:ViewModelLocator.AutoWireViewModel="true"
xmlns:userControls="using:M.Survey.UserControls"
mc:Ignorable="d"
xmlns:converters="using:M.Survey.Converters"
Loaded="pageRoot_Loaded"
Unloaded="pageRoot_Unloaded"
xmlns:templateSelectors="using:M.Survey.TemplateSelectors">
<prism:VisualStateAwarePage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/AllQuestionDataTemplates.xaml" />
<ResourceDictionary Source="/Themes/Styles.xaml" />
<ResourceDictionary Source="/Themes/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
<templateSelectors:QuestionTemplateSelector x:Key="QuestionDisplay"
TextEntry="{StaticResource TextEntry}"
DateTimeEntry="{StaticResource DateTimeEntry}"
BoolEntry="{StaticResource BoolEntry}"
IntEntry="{StaticResource IntEntry}"
SelectEntry="{StaticResource SelectEntry}"
DecimalEntry="{StaticResource DecimalEntry}"
LargeTextArea="{StaticResource LargeTextArea}"
SorEntry="{StaticResource SorEntry}"
Additional="{StaticResource AdditionalEntry}"
Miscellaneous="{StaticResource Miscellaneous}"
Dim="{StaticResource Dim}"
Signature="{StaticResource Signature}"
SectionNavigation="{StaticResource SectionNavigation}"/>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</ResourceDictionary>
</prism:VisualStateAwarePage.Resources>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{StaticResource ResourceKey=AppBackgroundColor}" />
</Grid.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="{StaticResource ResourceKey=MulalleyBlueBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Margin="20, 0, 0, 5">
<Button Margin="0, 0, 15, 0" Command="{Binding Path=TryGoHomeCommand}" Style="{StaticResource BackButtonStyle}" />
<TextBlock Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Text="{Binding Path=Job.ShortDisplayAddress}" VerticalAlignment="Bottom" />
<StackPanel Margin="40, 0, 0, 6" Width="30" VerticalAlignment="Bottom" Height="30">
<ProgressRing Height="30" Width="30" Foreground="White" IsActive="{Binding Path=WorkInProgress}" Visibility="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}" />
</StackPanel>
<TextBlock Margin="10, 0, 0, 10" VerticalAlignment="Bottom" Text="{Binding Path=FeedbackMsg}" Style="{StaticResource ResourceKey=SmallHeaderTextBlockStyle}" />
</StackPanel>
<Grid Margin="0, 0, 15, 5" Grid.Column="1" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Bottom" Text="Internet" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" />
<TextBlock Margin="10, 0, 15, 6" VerticalAlignment="Bottom" Grid.Column="1" Text="{Binding Path=ConnectionStatus}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
<TextBlock VerticalAlignment="Bottom" Text="Server" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Grid.Column="2" />
<TextBlock Margin="10, 0, 15, 6" VerticalAlignment="Bottom" Grid.Column="3" Text="{Binding Path=VpnStatus}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
<TextBlock VerticalAlignment="Bottom" Text="User" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}" Grid.Column="4" />
<TextBlock Margin="10, 0, 0, 6" VerticalAlignment="Bottom" Grid.Column="5" Text="{Binding Path=User.DisplayName}" Style="{StaticResource ResourceKey=SmallHeaderStyle}" />
</Grid>
<TextBlock Width="80" Margin="100, 0, 30, 0" Text="{Binding Path=AnswersCompleteDisplay}" Grid.Column="1" Style="{StaticResource ResourceKey=MediumHeaderTextBlockStyle}"/>
<Button Style="{StaticResource ResourceKey=SaveButtonStyle}" Content="Save" Grid.Column="2" Command="{Binding Path=SaveCommand}" />
</Grid>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{StaticResource ResourceKey=MulalleyBlue}" Opacity=".2" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListView
ItemsSource="{Binding Path=Survey.Sections, Mode=TwoWay}"
SelectedItem="{Binding Path=Survey.SelectedSection, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="Single"
ItemContainerStyle="{StaticResource ResourceKey=ListViewItemContainerStyle}"
ItemTemplate="{StaticResource ResourceKey=SurveySectionDataTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
<Grid Grid.Column="1" Margin="30, 30, 15, 20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Text="*" FontSize="40" FontWeight="Bold" Foreground="Red"/>
<TextBlock Text=" = Required " FontSize="20"/>
</StackPanel>
<ListView
x:Name="ListResponses"
Grid.Row="1"
ItemsSource="{Binding Path=Survey.SelectedSection.QuestionsAndNavigation, Mode=TwoWay}"
IsSwipeEnabled="False"
SelectionMode="None"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Background="White"
ItemTemplateSelector="{StaticResource ResourceKey=QuestionDisplay}"
ItemContainerStyle=
"{StaticResource ResourceKey=QuestionListViewItemContainerStyle}">
</ListView>
</Grid>
</Grid>
</Grid>
</Grid>
<userControls:SurveyDialogUserControl />
<userControls:SurveyReassignedDialogUserControl />
<userControls:CopyFromUserControl />
</Grid>
<prism:VisualStateAwarePage.BottomAppBar>
<CommandBar Name="commandBar">
<CommandBar.SecondaryCommands>
<AppBarButton Label="Settings" Icon="Setting" Click="AppBarButton_Click" />
<AppBarButton Label="Log" Icon="Admin" Click="LogButton_Click" />
<AppBarButton Label="Refresh" Icon="Refresh" Command="{Binding Path=RefershAllDataCommand}" IsEnabled="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=InverseBooleanConverter}}" Visibility="{Binding Path=VpnOnline, Converter={StaticResource ResourceKey=BooleanToVisibilityConverter}}" />
<AppBarButton Label="Copy From" Icon="Copy" Click="CopyButton_Click" Command="{Binding Path=ShowCopyFromCommand}" IsEnabled="{Binding Path=WorkInProgress, Converter={StaticResource ResourceKey=InverseBooleanConverter}}" />
</CommandBar.SecondaryCommands>
</CommandBar>
</prism:VisualStateAwarePage.BottomAppBar>
</prism:VisualStateAwarePage>
And this is the User control XAML;
<UserControl
x:Class="M.Survey.UserControls.SectionNavigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:M.Survey.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:M.Survey.Converters"
mc:Ignorable="d"
d:DesignHeight="30"
d:DesignWidth="400">
<UserControl.Resources>
<ResourceDictionary>
<converters:IntBooleanConverter x:Key="IntBooleanConverter" />
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Horizontal" >
<Button Margin="0,40,40,0"
IsEnabled="{Binding Path=PreviousSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenPreviousSectionCommand,
ElementName=ListResponses}"><< Previous Section</Button>
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId,
Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding
Path=DataContext.SaveThenNextSectionCommand,
ElementName=ListResponses}">Next Section >></Button>
</StackPanel>
</Grid>
</UserControl>
Assuming that the ListView XAML is in the same file as the Button XAML (and the ListView is x:Name="listView"):
<Button Margin="0,40,0,0"
IsEnabled="{Binding Path=NextSectionId, Converter={StaticResource ResourceKey=IntBooleanConverter}}"
Command="{Binding ElementName=listView, Path=DataContext.Survey.DESIRED_COMMAND}">Save and Next Section >></Button>
Additionally, you could also pass an instance of the Survey viewmodel into the question viewmodel, upon instantiation (depends on how you are creating your viewmodels).
Finally, you could also use an event publishing system (e.g. MVVM Light Messenger) to publish an event when the button is clicked that the Survey is wired to respond to.
I was helped a lot towards getting the answer from Eric, the guy who gives the other answer.
In order to get communication between 2 classes that are embedded in their own complicated designs, one way to do this is to use the EventAggregator pattern. As it happens the app I am working on uses Prism which as it's own implementation of EventAggregator. This pattern enables you to publish a payload which is picked up by subscribe events on the receiving class.
In a GroupBox I have two grids: the 1st one contains can contain lots of textboxes so that I added a ScrollViewer, the 2nd one contains action buttons that must allways be visible.
A 2nd GroupBox above has an Expander and the problem is that when it is expanded and the 1st grid has enough elements, it covers the 2nd grid with buttons. How can I fix this?
Here my code:
<Window x:Class="ControlOverlayBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="269" Width="454">
<DockPanel Background="LightGray" SnapsToDevicePixels="True">
<GroupBox DockPanel.Dock="Top" Header="Simulation Parameters">
<DockPanel>
<Expander DockPanel.Dock="Bottom" x:Name="AlgoParams" Header="Change Us...">
<Grid x:Name="parameterGrid">
<DataGrid
RowHeaderWidth="0"
ItemsSource="{Binding AlgoParameters}"
AutoGenerateColumns="False"
GridLinesVisibility="None"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="1*" IsReadOnly="True"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="1*"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="1*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Expander>
</DockPanel>
</GroupBox>
<GroupBox DockPanel.Dock="Bottom" Header="Other Parameters">
<DockPanel x:Name="BindPanel" VerticalAlignment="Stretch">
<ScrollViewer VerticalScrollBarVisibility="Auto" DockPanel.Dock="Top" >
<Grid x:Name="paramGrid" Margin="3" Background="Yellow">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="200*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="363*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<GridSplitter Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ResizeDirection="Columns" Width="5" Height="Auto" ShowsPreview="false"/>
<Label Content="ID" FontWeight="Bold" HorizontalAlignment="Center"/>
<Label Content="Name" Grid.Column="1" FontWeight="Bold" HorizontalAlignment="Left"/>
<Label Content="Values" Grid.Column="3" FontWeight="Bold" HorizontalAlignment="Left"/>
<TextBlock Grid.Row="1" Text="1"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="p10"/>
<TextBlock Grid.Row="1" Grid.Column="4" Text="bla"/>
<TextBlock Grid.Row="2" Text="2"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="p11"/>
<TextBlock Grid.Row="2" Grid.Column="4" Text="blub"/>
</Grid>
</ScrollViewer>
<Grid DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Background="AliceBlue" MinHeight="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="123"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Content="_Load Settings" HorizontalAlignment="Left"/>
<Button Content="_Save Settings" Grid.Column="1" HorizontalAlignment="Left"/>
<Button Content="_Run Simulation" Grid.Column="2" HorizontalAlignment="Right"/>
</Grid>
</DockPanel>
</GroupBox>
</DockPanel>
Thanks in advance!
Give your ScrollViewer a MaxHeight to get the scroll working instead of overlapping
Well, I solved the problem by changing the DockPanel (named BindPanel) by a Grid with two rows (the 2nd row has a fixed height) and it works. Here the interesting part:
<GroupBox DockPanel.Dock="Bottom" Header="Other Parameters">
<Grid x:Name="BindPanel" VerticalAlignment="Stretch" Background="Beige">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="24"/>
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto" VerticalAlignment="Top">
...
</ScrollViewer>
<!-- grid wih buttons -->
</Grid>
</GroupBox>
I'm very new to WPF so go easy
I have a bound list box which is bounce to a datasource.
I have created a data template for the items in the list box and some of the columns of data need to be editable. to make them editable I have created a bound textbox in a data template in a resource file.
My question is. Once I am in the textbox and have edited the data. How do I then use the keyboard to arrow down to the next line and have it automatically give focus to the same textbox on the next line down.
I have been searching for 2 days now but no solution seems to fit my problem
Template in resource file;
<DataTemplate x:Key="myTemplate" x:Name="dTemplate">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=b2b_StockCode}" Control.Template="{StaticResource lvTemplate}" Width="80"/>
<Label Content="{Binding Path=b2b_PartNo}" Control.Template="{StaticResource lvTemplate}" Width="70"/>
<Label Content="{Binding Path=b2b_desc}" Control.Template="{StaticResource lvTemplate}" Width="245"/>
<TextBox x:Name="liProfileStockQty" HorizontalContentAlignment="Right" Text="{Binding Path=b2b_ProfileStockQty}" Control.Template="{StaticResource lvTextBoxTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_StockQty}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_DBCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_InternetCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_PerCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_TotalCost}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Separator Width="2"/>
<Label Content="{Binding Path=b2b_TradePrice}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<Label Content="{Binding Path=b2b_PerTrade}" Control.Template="{StaticResource lvLabelRightAlignTemplate}" Width="50"/>
<TextBox x:Name="liDiscountTrade" HorizontalContentAlignment="Right" Text="{Binding Path=b2b_DiscountTrade}" Control.Template="{StaticResource lvTextBoxTemplate}" Width="50"/>
</StackPanel>
</DataTemplate>
my Main Xaml looks like this...
<Window x:Class="B2BPricing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:my="clr-namespace:B2BPricing"
Title="B2B Pricing" Height="768" Width="1024" WindowStartupLocation="CenterScreen"
WindowStyle="ThreeDBorderWindow" ResizeMode="CanMinimize" Icon="/B2BPricing;component/NewCEF.ico" >
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ListViewResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<my:B2BPricingDataSet x:Key="b2bPricing" />
<CollectionViewSource x:Key="b2bPricingViewSource" Source="{Binding Path=B2bPricing, Source={StaticResource b2bPricing}}" />
<CollectionViewSource x:Key="b2bPricingsupplierViewSource" Source="{Binding Path=B2bPricing_supplier, Source={StaticResource b2bPricingViewSource}}" />
</ResourceDictionary>
</Window.Resources>
<Grid DataContext="{StaticResource b2bPricingViewSource}" Name="MainGrid" >
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="836,12,0,0" Name="resultLabel" VerticalAlignment="Top" />
<Label Content="Stock Code" Height="28" HorizontalAlignment="Left" Margin="13,34,0,0" Name="lblStockCode" VerticalAlignment="Top" />
<Label Content="Part No." Height="28" HorizontalAlignment="Right" Margin="0,34,864,0" Name="lblPartNo" VerticalAlignment="Top" />
<Label Content="Description" Height="28" HorizontalAlignment="Right" Margin="0,34,776,0" Name="lblDescription" VerticalAlignment="Top" />
<Grid HorizontalAlignment="Left" Margin="51,609,0,0" Name="grid2" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Stock Code" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="b2b_StockCodeTextBox" Text="{Binding Path=b2b_StockCode, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
</Grid>
<Grid HorizontalAlignment="Left" Margin="51,644,0,0" Name="grid1" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Description" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="b2b_descTextBox" Text="{Binding Path=b2b_desc, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="386" />
</Grid>
<Grid HorizontalAlignment="Left" Margin="297,609,0,0" Name="grdSupplier" VerticalAlignment="Top" Width="219">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Supplier" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Height="22" HorizontalAlignment="Left" Margin="3,4,0,6" Name="tbSupplier" VerticalAlignment="Center" Width="150" Text="159" />
</Grid>
<ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource myTemplate}" Height="542" HorizontalAlignment="Stretch"
Margin="0,60,0,0" Name="lbPricing" VerticalAlignment="Top" Width="983" IsManipulationEnabled="True"
SelectionMode="Extended" IsSynchronizedWithCurrentItem="True"
KeyboardNavigation.TabNavigation="Continue"
KeyboardNavigation.DirectionalNavigation="Once">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Label Content="{Binding Path=Name}" Padding="4" Background="{StaticResource myGroupingBrush}" Foreground="White">
</Label>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
<Button Content="_Load" Height="23" HorizontalAlignment="Left" Margin="52,694,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<ProgressBar HorizontalAlignment="Stretch" Margin="230,311,246,385" Name="progressBar1" VerticalAlignment="Stretch" Width="526" Visibility="Visible" IsIndeterminate="False" />
<Grid DataContext="{StaticResource b2bPricingsupplierViewSource}" HorizontalAlignment="Left" Margin="12,-3,0,0" Name="grid4" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Supplier: " Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" FlowDirection="LeftToRight" />
<Label Content="{Binding Path=sup_fullname}" Grid.Column="1" Grid.Row="0" Height="28" HorizontalAlignment="Left" Margin="3" Name="sup_fullnameLabel" VerticalAlignment="Center" />
</Grid>
<Button Content="_Show" Height="23" HorizontalAlignment="Left" Margin="156,694,0,0" Name="btnTextBoxContents" VerticalAlignment="Top" Width="75" Click="btnTextBoxContents_Click" />
</Grid>
and the c#:
private void btnTextBoxContents_Click(object sender, RoutedEventArgs e)
{
ListBoxItem item = (lbPricing.SelectedItem as ListBoxItem);
TextBox tb = (TextBox)lbPricing.ItemsPanel.FindName("liProfileStockQty", lbPricing);
//TextBox tb = dTemplate.FindName("liProfileStockQty") as TextBox;
resultLabel.Content = tb.Text;
}
This is not the only way I've tried. I also tried copying something like this.
foreach (LinePosition item in this.ListLinePositions.Items)
{
CheckBox cb = FindByName("checkedPosition",
(ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;
}
but replacing that examples checkbox with the textbox control i'm tring to find
I've got 2 listbox and one scrollviewer and I want the scrollviewer to scroll the two listbox together. But i don't know how to do.. Here's my xaml :
<ScrollViewer Grid.Row="1">
<Grid>
<ListBox Name="listboxRSSFeedItems" Width="240" Height="644" Margin="0,0,240,0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding Title}" Grid.Row="0" FontSize="24" HorizontalAlignment="Left" />
<HyperlinkButton Content="Link to details" NavigateUri="{Binding Link}" HorizontalAlignment="Left" Grid.Row="1" Margin="0,0,0,30" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Name="listboxRSSFeedItems2" Width="240" Height="644" Margin="240,0,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding Title}" Grid.Row="0" FontSize="24" HorizontalAlignment="Left" />
<HyperlinkButton Content="Link to details" NavigateUri="{Binding Link}" HorizontalAlignment="Left" Grid.Row="1" Margin="0,0,0,30" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</ScrollViewer>
Thanks a lot
Ok. I just tried to do one listbox with grid. it works fine, but how to choose which grid to add my item.
I used to add with "listboxRSSFeedItems.Items.Add(item)", but now, how can i choose the 2nd Column only.
<ScrollViewer Grid.Row="1">
<ListBox x:Name="listboxRSSFeedItems" Width="480" Height="680">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid x:Name="first" Grid.Column="0">
<TextBlock TextWrapping="Wrap" Text="{Binding Title}" />
<HyperlinkButton NavigateUri="{Binding URL}" TargetName="_blank"/>
</Grid>
<Grid x:Name="second" Grid.Column="1">
<TextBlock TextWrapping="Wrap" Text="{Binding Title}" />
<HyperlinkButton NavigateUri="{Binding URL}" TargetName="_blank" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
Thanks again
You shouldn't set height of your listBoxes.
ps: Consider layout using columns
<ScrollViewer >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox x:Name="first" Grid.Column="0" />
<ListBox x:Name="second" Grid.Column="1" />
</Grid>
</ScrollViewer>
Set VerticalScrollBarVisibility to "Disabled" for listBoxes.
And it will be better using StackPanel for this puprose instead of Grid.
To display a WPF list box, I use a user control (MessageRowTemplate) as list box item. The user contrul uses a value converter (MessageTypeToBrushConverter). For some reason, the value converter is only trigered if the user control isn't used as list box item but seperate.
xaml-file with user control used seperately and inside a list box as list box item:
<local:MuuriWindow x:Class="Muuri_UI_WPFv3.Window_Conversation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Muuri_UI_WPFv3"
Loaded="EventHandler_WindowLoaded">
<Window.Resources>
<DataTemplate x:Key="ResponsesListTemplate">
<local:MessageRowTemplate Margin="3" />
</DataTemplate>
<local:MessageTypeToBrushConverter x:Key="converter" />
</Window.Resources>
<StackPanel Orientation="Vertical">
<local:MessageRowTemplate x:Name="questionControl" />
<ListBox HorizontalAlignment="Stretch" ItemTemplate="{DynamicResource ResponsesListTemplate}" ItemsSource="{Binding}" Name="listBoxResponses" Height="200" ScrollViewer.VerticalScrollBarVisibility="Visible" Style="{StaticResource MuuriListboxStyle}" />
<StackPanel Orientation="Horizontal">
<Button Name="buttonReply" Click="EventHandler_ButtonReplyClicked">Reply</Button>
<Button Name="buttonCancel" Click="EventHandler_ButtonCancelClicked">Cancel</Button>
</StackPanel>
</StackPanel>
</local:MuuriWindow>
And here's the MessageRowTemplate.xaml:
<UserControl x:Class="Muuri_UI_WPFv3.MessageRowTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Muuri_UI_WPFv3"
Width="300"
Loaded="UserControl_Loaded">
<UserControl.Resources>
<local:MessageTypeToBrushConverter x:Key="converter" />
</UserControl.Resources>
<Grid Margin="2,1,2,1" VerticalAlignment="Top" Height="auto">
<Grid.RowDefinitions>
<RowDefinition Height="2" />
<RowDefinition Height="0.5*" />
<RowDefinition Height="0.5*" />
<RowDefinition Height="2" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Name="leftBorder" Margin="2,2,0,2" Grid.RowSpan="4" CornerRadius="3,0,0,3" Panel.ZIndex="1" Background="{Binding Path=Type, Converter={StaticResource converter}}" />
<Border Margin="0,0,0,0" Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="4" Background="#FF666666" CornerRadius="0,5,5,0" />
<Border Margin="0,0,0,0" Grid.ColumnSpan="2" Grid.RowSpan="4" Background="{x:Null}" BorderThickness="2,2,2,2" BorderBrush="#FF202020" CornerRadius="5,5,5,5" Panel.ZIndex="0" />
<TextBlock Padding="5,2,5,2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Text="{Binding Path=Text}" Grid.Row="1" Grid.Column="1" Foreground="White" />
<TextBlock Padding="5,2,5,2" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Path=Author}" Grid.Row="2" Grid.Column="1" Foreground="White" />
</Grid>
</UserControl>
As you might guess, the value converter is responsible for converting some value to a brush. It works fine for this <local:MessageRowTemplate x:Name="questionControl" /> but not for the list box item.
Google didn't give me any hints for keyword combinations of 'list box', 'list box item', 'user control', 'value converter'.
Do you have an idea?
Thanks in advace.
The list box
<ListBox HorizontalAlignment="Stretch" ItemTemplate="{DynamicResource ResponsesListTemplate}" ItemsSource="{Binding}" Name="listBoxResponses" Height="200" ScrollViewer.VerticalScrollBarVisibility="Visible" Style="{StaticResource MuuriListboxStyle}" />
uses the local resource ResponsesListTemplat which again uses the MessageRowTemplate.
The converter is used within this template:
<UserControl.Resources>
<local:MessageTypeToBrushConverter x:Key="converter" />
</UserControl.Resources>
and applied to the first border-tag within the template.