'System.InvalidOperationException' in TabControl - wpf

<Window x:Class="AutoMerge2013.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AutoMerge2013" Height="350" Width="525"
Style="{StaticResource VS2012WindowStyle}">
<TabControl x:Name="tbControl">
<TabItem Header="Main">
<Canvas>
<Button Name="btnOpen" Content="Open files" Canvas.Left="40" Canvas.Top="20" Width="98" Click="btnOpen_Click_1"/>
<Button Name="btnUpload" Content="Upload files" Canvas.Left="181" Canvas.Top="20" Width="98" Visibility="Hidden" Click="btnUpload_Click_1"/>
<Button Name="btnMerge" Content="AutoMerge" Canvas.Left="338" Canvas.Top="20" Width="98" Visibility="Hidden" Click="btnMerge_Click_1"/>
<StatusBar Canvas.Top="247" Width="499">
<TextBlock x:Name="txtInfo"></TextBlock>
<StatusBarItem HorizontalAlignment="Right">
<ResizeGrip />
</StatusBarItem>
</StatusBar>
</TabItem>
<TabItem Header="Original">
</TabItem>
</TabControl>
When I click the Original tab it throws an exeption:
An unhandled exception of type 'System.InvalidOperationException'
occurred in PresentationCore.dll
Additional information: The specified Visual is not a descendant of this Visual.
I use in my application Style="{StaticResource VS2012WindowStyle}"
Maybe it is an issue...
Can you give some advise how to avoid this exception and save my style ?

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>

'Type Not Defined' Error After Adding x:Name Attribute To User Control Declaration Inside A User Control

I am having trouble with the Xaml passer throwing a 'Type Not Defined' exception after I add a x:Name attribute to a user control declaration which is inside another user control.
<UserControl x:Class="LoginView"
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:vm="clr-namespace:PowderKegSoftware.Artisan.Client.Authentication"
xmlns:ctrl="clr-namespace:PowderKegSoftware.Artisan.Client.Ui.Controls;assembly=CommunalUi"
xmlns:auth="clr-namespace:PowderKegSoftware.Artisan.Client.Authentication"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:LoginViewmodel,
IsDesignTimeCreatable=False}"
d:DesignHeight="693" d:DesignWidth="1080">
<UserControl.Resources>
---------------------
</UserControl.Resources>
------------------------
<Border BorderThickness="0">
<Border.Background>
<ImageBrush ImageSource="{Binding BackgroundImage}"
Stretch="None" />
<TextBlock Grid.Column="0"
Grid.Row="1"
Text="{Binding UsernameLabelText}"
Style="{StaticResource TextBlockBaseStyle}"/>
<ctrl:ArtisanComboBox Grid.Column="0"
Grid.Row="2"
Height="30"
TabIndex="10"
Style="{StaticResource ArtisanComboBoxDefaultStyle}"
DataContext="{Binding UsernamesVm}" />
<TextBlock Grid.Column="0"
Grid.Row="3"
Text="{Binding PasswordLabelText}"
Style="{StaticResource TextBlockBaseStyle}"
Margin="0 20 0 0"/>
------------------
This all works just fine until I try and add the x:Name attribute to the <ctrl:ArtisanComboBox ... declaration.
<ctrl:ArtisanComboBox Grid.Column="0"
Grid.Row="2"
x:Name="ChoosableUserNames" <!-- Throws a 'Type Not Defined' error. -->
Height="30"
TabIndex="10"
Style="{StaticResource ArtisanComboBoxDefaultStyle}"
DataContext="{Binding UsernamesVm}" />
I have tried using x:Name= and just Name= but neither complies. I want to name this declaration so I can use FocusManager.FocusedElement="{Binding ElementName=ChoosableUserNames} to set the focus on this control when the view is renered.
Thanks for any help you can give.

C# WPF XAML A first chance exception of type ... occurred in PresentationFramework.dll

I am trying to design an application I am working on and I'm working on the XAML. There is a DockPanel and inside it is a StackPanel docked to the left and another to the right. I keep getting the error A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll. The error is happening on the line where the ImageBrush tag is. Why is this happening?
Here is my XAML:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="730" Width="1220">
<DockPanel>
<Border BorderBrush="#FF5AD8E5" BorderThickness="0,0,3,0">
<StackPanel DockPanel.Dock="Left" Width="208">
<StackPanel.Background>
<ImageBrush ImageSource="/assets/img/nav_primary.png"></ImageBrush>
</StackPanel.Background>
<Label Content="Welcome, Scott!" Foreground="White" HorizontalAlignment="Right" FontSize="16" Padding="5,15,15,5" OpacityMask="Black" />
</StackPanel>
</Border>
<StackPanel DockPanel.Dock="Right" Width="Auto">
</StackPanel>
</DockPanel>
</Window>

Re-using UI in WPF from a Resource

I have a several tabs on UI where on each tab I need the below:
<StackPanel Orientation="Horizontal">
<Button Content="<" Command="{Binding MoveToPreviousCommand}" />
<TextBlock Text="{Binding SelectedItem.Name}" />
<Button Content=">" Command="{Binding MoveToNextCommand}" />
</StackPanel>
Is there a way to avoid replicating this code in XAML for each tab by specifying the above for example in the Resources only once and pointing into that resource under each tab?
Using ContentControl
<Window.Resources>
<StackPanel x:Key="Content" x:Shared="False" Orientation="Horizontal">
<Button Content="<" Command="{Binding MoveToPreviousCommand}" />
<TextBlock Text="{Binding SelectedItem.Name}" />
<Button Content=">" Command="{Binding MoveToNextCommand}" />
</StackPanel>
<DataTemplate x:Key="template1">
<StackPanel Orientation="Horizontal">
<Button Content="<" Command="{Binding MoveToPreviousCommand}" />
<TextBlock Text="{Binding SelectedItem.Name}" />
<Button Content=">" Command="{Binding MoveToNextCommand}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ContentControl Content="{StaticResource Content}"></ContentControl>
<ContentControl Name="contCtrl" ContentTemplate="{StaticResource template1}" Content="This is the content of the content control."/>
</StackPanel>
using usercontrol
<UserControl x:Class="WpfApplication2.UserControl1"
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="300" d:DesignWidth="300">
<StackPanel Orientation="Horizontal">
<Button Content="<" Command="{Binding MoveToPreviousCommand}" />
<TextBlock Text="{Binding SelectedItem.Name}" />
<Button Content=">" Command="{Binding MoveToNextCommand}" />
</StackPanel>
<Window x:Class="WpfApplication2.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"
xmlns:local="clr-namespace:WpfApplication2">
<Grid>
<local:UserControl1></local:UserControl1>
</Grid>

Setting Focus Does Not Allow Instant Editing

I have a WPF UserControl with a textbox. Here's how the textbox and it's parent control are defined:
<DockPanel Margin="10,20,10,10" FocusManager.FocusedElement="{Binding ElementName=uxJobNumber}" >
<TextBox x:Name="uxJobNumber" Text="{Binding JobNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" TextWrapping="Wrap" FontSize="48" BorderBrush="Black" BorderThickness="1" Margin="10"/>
</DockPanel>
With the FocusManager.FocusedElement set, I can see a cursor bar present within the textbox. However, the cursor bar is not blinking, and does not allow the user to immediately start typing.
Without the FocusManager.FocusedElement set, when the application starts there is no cursor bar within the text box at all.
Here's the complete XAML
<UserControl x:Class=""
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:converters="clr-namespace:.Modules.Converters"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
mc:Ignorable="d">
<UserControl.Resources>
<converters:VisibilityConverter x:Key="Visibility" />
</UserControl.Resources>
<Canvas Width="1024" Height="768" >
<Border Style="{DynamicResource GroupBox}" Canvas.Left="36.261" Canvas.Top="32.131" Width="426.936">
<StackPanel>
<Border>
<TextBlock Text="STEP 1"/>
</Border>
<TextBlock Text="Enter the five (5) digit Job Number and click Verify." />
<Path/>
<DockPanel Margin="10,20,10,10" FocusManager.FocusedElement="{Binding ElementName=uxJobNumber}" >
<Button Content="Verify" Width="125" Height="65" HorizontalAlignment="Right" Command="{Binding SearchJobCommand}" Style="{DynamicResource RedButton}" Margin="0" DockPanel.Dock="Right" IsDefault="True"/>
<TextBox Text="{Binding JobNumber, Mode=TwoWay, ValidatesOnDataErrors=True}" TextWrapping="Wrap" FontSize="48" BorderBrush="Black" BorderThickness="1" x:Name="uxJobNumber" Margin="10" KeyboardNavigation.TabIndex="0" />
</DockPanel>
</StackPanel>
</Border>
<TextBlock Text="{Binding Error}" Visibility="{Binding HasError, Converter={StaticResource Visibility}}" Canvas.Left="48" Canvas.Top="288" FontSize="16" Width="403" Foreground="Red" />
</Canvas>
We finally resorted to using the Focus() method in the code behind when the form is done loading.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
uxJobNumber.Focus();
}
Using your code I get this to work; blinking and all.

Resources