Why the datagrid is not shown? - wpf

I have a main windows with a button that open another window that has a user control and this user control has a datagrid. The code is this:
Main windows: code of the button:
dlgImprimirControlUsuarioView miView = new dlgImprimirControlUsuarioView();
miView.Show();
The code in my second window (codebehind):
public partial class dlgImprimirControlUsuarioView : Window
{
public dlgImprimirControlUsuarioView()
{
InitializeComponent();
UserControlView miView = new UserControlView();
this.ccControlUsuario = miView;
}
}
AXML of the second windows:
<Window x:Class="WpfObtenerTamañoControles.dlgImprimirControlUsuarioView"
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:WpfObtenerTamañoControles"
mc:Ignorable="d"
Title="dlgImprimirControlUsuario" Height="200" Width="300">
<Grid>
<ScrollViewer Name="svControlUsuario" HorizontalAlignment="Left" Height="200" Width="300" Margin="0,0,0,0" VerticalAlignment="Top" >
<ContentControl Name="ccControlUsuario" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
</ScrollViewer>
</Grid>
</Window>
The AXML of the user control that is used by the second windows:
<UserControl x:Class="WpfObtenerTamañoControles.UserControlView"
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:local="clr-namespace:WpfObtenerTamañoControles"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="0,0,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<ScrollViewer Name="svDatagrid" HorizontalAlignment="Left" Height="32" Margin="0,0,0,0" VerticalAlignment="Top" Width="300">
<DataGrid Name="dgDatagrid" HorizontalAlignment="Left" Height="120" Margin="0,0,0,0" VerticalAlignment="Top" Width="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Contador" Binding="{Binding}" Width="2.5cm"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</UserControl>
I can see the vertical scroll bar of the scroll viewer of the second window, but I can't see the content of this scroll viewer. But I am not able to see the problem.
Thanks.

Related

Collapsed Tab Item Prevents Control Select in WPF Designer

I'm using Visual Studio Pro 2019, and I came across an issue when using the tab control. I created a test project to test switching between collapsed tabs programmatically, but found that, when in the designer, I'm unable to select the control within the tab when the tabs are collapsed. I found this answer confirming that the tab being collapsed is the issue.
My question is whether there is a fix for this that isn't just turning the tabs to visible, as it shifts the item's grid location.
The alternate solution could be to manually alternate the visibility of overlapping grids, but tabs seem to be the more appropriate solution.
xaml:
<Window x:Class="MainWindow"
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:delete3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="#FF424242">
<Grid>
<TabControl Name="Tabs" HorizontalAlignment="Left" Height="255" Margin="122,51,0,0" VerticalAlignment="Top" Width="437">
<TabItem Name="First" Header="TabItem" Visibility="Collapsed">
<Grid Background="#FFE5E5E5">
<TextBox HorizontalAlignment="Left" Height="23" Margin="143,129,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="29"/>
</Grid>
</TabItem>
<TabItem Name="Second" Header="TabItem" Visibility="Collapsed">
<Grid Background="#FFE5E5E5">
<TextBox HorizontalAlignment="Left" Height="23" Margin="195,90,0,0" TextWrapping="Wrap" Text="2" VerticalAlignment="Top" Width="29"/>
</Grid>
</TabItem>
</TabControl>
<Button Content="1" HorizontalAlignment="Left" Margin="191,359,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="2" HorizontalAlignment="Left" Margin="484,379,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>

WPF Combobox SelectionChanged Event is not firing

I have a comboBox, user controls and a button. SelectionChanged event is not firing in my UI App but when I copy the same combobox code in a different WPF app, selectionChanged event is firing. I don't understand why event is not triggered just here.
<Window x:Class="ExecutionInformation.TesterApp.MainWindow"
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:ExecutionInformation.TesterApp.UserControls"
mc:Ignorable="d"
Title="MainWindow" Height="Auto" Width="800">
<ScrollViewer>
<Grid>
<StackPanel>
<Label Content="Execution Information service" Margin="20,10,20,10"
HorizontalAlignment="Center" FontSize="15" FontFamily="Bold"/>
</StackPanel>
<StackPanel Margin="20">
<!--<ComboBox Name="CmbAPIList" ItemsSource="{StaticResource ExecInfoServiceApis}"
DisplayMemberPath ="ValueString"
SelectedValuePath="ValueString" Margin="100,20,100,0"
SelectedValue="{Binding ColorString}" SelectionChanged="CmbAPIList_SelectionChanged"/>-->
<ComboBox Name="CmbAPIList"
SelectionChanged="CmbAPIList_SelectionChanged">
<ComboBoxItem>ApproveTestStartAsync</ComboBoxItem>
<ComboBoxItem>CompleteTestAsync</ComboBoxItem>
<ComboBoxItem>GetPartAsync</ComboBoxItem>
<ComboBoxItem>GetAssemblyAsync</ComboBoxItem>
<ComboBoxItem>PutAssemblyAttributesAsync</ComboBoxItem>
</ComboBox>
<local:ApproveTestStartUC x:Name="approveTestStartUC" Visibility="Collapsed" Margin="20,10,20,10" Background="Cyan" />
<local:CompleteTestUC x:Name="completeTestUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:GetPartUC x:Name="getPartUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:GetAssembly x:Name="getAssemblyUC" Visibility="Collapsed" Margin="20,10,20,10" />
<local:PutAssemblyAttributesUC x:Name="putAssemblyUC" Visibility="Collapsed" Margin="20,10,20,10"/>
<WrapPanel Margin="15" HorizontalAlignment="Center">
<Button Name="btnShowResults" Click="btnShowResults_Click" Visibility="Collapsed" Width="100">Show Results</Button>
</WrapPanel>
<Border BorderThickness="1" BorderBrush="LightGreen">
<TextBlock Name="TxtResults" Visibility="Collapsed"></TextBlock>
</Border>
</StackPanel>
</Grid>
</ScrollViewer>
Commented ComboBox code in the above code works without any issues.
Thanks

Strange behaviour of scrollviewer in tabcontrol

I have following code:
<Window x:Class="WpfApplication5.MainWindow"
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:WpfApplication5"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabControl.Items>
<TabItem>
<TabItem.Header>tabItem1</TabItem.Header>
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Disabled">
<WrapPanel>
<Image Source="RentgenogramOfWrist.jpg" Width="500"></Image>
<Button Width="300" Height="300"/>
</WrapPanel>
</ScrollViewer>
</TabItem.Content>
</TabItem>
<TabItem>
<TabItem.Header>tabItem2</TabItem.Header>
<TabItem.Content>
<ScrollViewer HorizontalScrollBarVisibility="Disabled">
<WrapPanel>
<Image Source="RentgenogramOfWrist.jpg" Width="500"></Image>
<Button Width="300" Height="300"/>
</WrapPanel>
</ScrollViewer>
</TabItem.Content>
</TabItem>
</TabControl.Items>
</TabControl>
</Grid>
The problem is that when program is running and I switch to second tabItem scrollViewer will be scrolled to start of button. When I now switch from second tabItem to first one the same happenes.
The Button's Focusable property is true by default, so the ScrollViewer scrolls to focus the button.
Set
<Button Width="300" Height="300" Focusable="False"/>
for one of them and see what changes.

WPF ListBox with bottom-alignment displays at top within StackPanel

I have the following XAML:
<Window x:Class="test_stacking.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Background="AliceBlue">
<Canvas Background="Red">
</Canvas>
<ListBox Width="200" VerticalAlignment="Bottom">
<TextBlock Text="One" />
<TextBlock Text="Two" />
</ListBox>
</StackPanel>
</Window>
I would like the Canvas to be on top, and the ListBox to be on the bottom. Since the default orientation for a StackPanel is vertical, I thought I would get the Canvas and the ListBox, in that order, stacked within the StackPanel.
But instead, I get what is shown below: the ListBox is on top, and the Canvas does not show at all. What am I doing wrong?
.NET FW 4 Client Profile, Windows 7, VS 2010.
If it is not mandatory to use StackPanel then you can achieve that by using Grid's * sizing.
Here is an example:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="500" Width="500">
<Grid Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Canvas Background="Red">
</Canvas>
<ListBox Grid.Row="1" Width="200" VerticalAlignment="Bottom">
<TextBlock Text="One" />
<TextBlock Text="Two" />
</ListBox>
</Grid>
</Window>
Output:
Or
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="500" Width="500">
<Grid Background="AliceBlue">
<Canvas Background="Red">
</Canvas>
<ListBox Width="200" VerticalAlignment="Bottom">
<TextBlock Text="One" />
<TextBlock Text="Two" />
</ListBox>
</Grid>
</Window>
Output:
Since you haven't set any height or width to the canvas, the height and width for the canvas is set to zero and thats why its not shown in the UI.
Try this
<StackPanel Background="AliceBlue">
<Canvas Background="Red" Width="200" Height="200">
</Canvas>
<ListBox Width="200" VerticalAlignment="Bottom">
<TextBlock Text="One" />
<TextBlock Text="Two" />
</ListBox>
</StackPanel>

StackPanel visibility VS Grid Visibility

I am using MVVM application and there is something that don't understand..
What is the difference between StackPanel visibility and Grid Visibility.
if I have this Grid...
<UserControl x:Class="Envitech.Setup.Presentation.Views.MonitorScreenViews.MonitorAlertViews.MonitorAlertView" 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" mc:Ignorable="d" d:DesignHeight="438" d:DesignWidth="842" xmlns:popup="clr-namespace:Envitech.Setup.Presentation.Views.GlobalViews">
<DockPanel DataContext="{Binding MonitorAlertViewModel}" Width="824" HorizontalAlignment="Left" VerticalAlignment="Top" Height="435">
<Grid DataContext="{Binding CurrentMonitorAlert}" Height="422" Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
<Label Content="Value" Height="28" HorizontalAlignment="Left" Margin="10,103,0,0" VerticalAlignment="Top" />
</Grid>
</DockPanel>
</UserControl>
visibility does not work, but if i do it this way...
<UserControl x:Class="Envitech.Setup.Presentation.Views.MonitorScreenViews.MonitorAlertViews.MonitorAlertView" 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" mc:Ignorable="d" d:DesignHeight="438" d:DesignWidth="842" xmlns:popup="clr-namespace:Envitech.Setup.Presentation.Views.GlobalViews">
<DockPanel DataContext="{Binding MonitorAlertViewModel}" Width="824" HorizontalAlignment="Left" VerticalAlignment="Top" Height="435">
<StackPanel Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
<Grid DataContext="{Binding CurrentMonitorAlert}" Height="422">
<Label Content="Value" Height="28" HorizontalAlignment="Left" Margin="10,103,0,0" VerticalAlignment="Top" />
</Grid>
</StackPanel>
</DockPanel>
</UserControl>
visibility works just fine.
Why?
The DataContext of the Grid is CurrentMonitorAlert. The DataContext of the StackPanel is MonitorAlertViewModel. Thus, the binding to NoMonitorsMessageVisibility is resolving against the wrong thing in your Grid case.
Setting the DataContext like that all over your view is somewhat unorthodox. Normally when doing MVVM you let WPF handle setting the DataContext (except possibly at the root level) and use deeper paths in your bindings if necessary. You might want to consider taking that approach.

Resources