Error template not showing for controls within a page - c#

I have a textbox within a page that binds to a model property that is validated, and when there is an error the error template is not shown!
A few points:
I have no validation issues for controls in there own window (not within a page displayed in a frame within a window).
The error template works, it is displayed for controls within a window.
The model is validating because the "Save" button within the page is disabled when a validation error is identified.
I "think" the problem lies with the fact that the control is within a page that is hosted within frame and thus the data context is not being passed to the page as it is seemingly isolated. Could that be the case? and if so how do I go about fixing this issue? and if not what else could it be?
The code (I have of course simplified the code to isolate the issue):
<Page x:Class="PIRS_Client.View.Staff.StaffDetailsView"
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:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
DataContext="{Binding StaffDetailsVM, Source={StaticResource Locator}}"
Height="576" Width="1163">
<Grid>
<TextBox HorizontalAlignment="Left" Text="{Binding Model.title, ValidatesOnDataErrors=True}" Height="17" Margin="284,453,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"/>
<Button Content="Save Changes" Command="{Binding SaveDetailsCommand}" IsEnabled="{Binding Model.IsValid}" HorizontalAlignment="Left" Margin="1007,518,0,0" VerticalAlignment="Top" Width="104" Height="23"/>
</Grid>
If I can add any further information or code please just let me know!

Problem solved - it transpires that Windows have a built in adorner layer but Pages do not, I would love to give you a detailed explanation as to why not but I don't know why (read below). But the solution is that you need to wrap your page contents in an adorner decorator to provide them with an adorner layer.
Just to be clear here is the "fixed" code:
<Page x:Class="PIRS_Client.View.Staff.StaffDetailsView"
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:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
DataContext="{Binding StaffDetailsVM, Source={StaticResource Locator}}"
Height="576" Width="1163">
<AdornerDecorator>
<Grid>
<TextBox HorizontalAlignment="Left" Text="{Binding Model.title, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="17" Margin="284,453,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"/>
<Button Content="Save Changes" Command="{Binding SaveDetailsCommand}" IsEnabled="{Binding Model.IsValid}" HorizontalAlignment="Left" Margin="1007,518,0,0" VerticalAlignment="Top" Width="104" Height="23"/>
</Grid>
</AdornerDecorator>
If you know why pages don't implement the adorner layer and can provide a more insightful answer please feel free to add an explanation as another answer which contains the solution i.e. this and the explanation and I will change your answer to be the correct answer (instead of mine) - just to promote sharing knowledge ;)

Related

How to select a TextBox with gaze?

I want to build a "gaze enabled" app, where most controls could be activated just using one's gaze.
Doing that with a button is pretty easy. Just add gaze:GazeInteraction.Interaction = Enable to the button in XAML and when I dwell at it it's as if I've clicked on it with the cursor.
But it doesn't work with a TextBox that way. Maybe I should do something from code-behind?
I'm still a beginner with C# and I've already read this, but I coldn't understand it well, so I used that instead.
Here's my code so far:
<Page
x:Class="gazebox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:gazebox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:gaze="using:Microsoft.Toolkit.Uwp.Input.GazeInteraction"
Width="500" Height="250">
<Grid Background="#FF1A3163" VerticalAlignment="Stretch">
<Button Content="Exit" HorizontalAlignment="Right" Margin="0,0,25,0" Height="100" Width="100" Background="#66E8E5E5" gaze:GazeInput.Interaction="Enabled"/>
<TextBox x:Name="textBox" Text="" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" Width="200" Margin="25,150,0,0" gaze:GazeInput.Interaction="Enabled"/>
<TextBox x:Name="textBox2" Text="" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" Width="200" Margin="25,50,0,0"/>
</Grid>
</Page>
and my .cs file is pretty much empty yet.
So, how do I make the TextBox(or any other element for that matter) be clicked when I dwell at it?
Any help would be greatly appreciated.
Pavel
p.s. I have a Tobii eye tracker

Is there a way to display WPF Extended Toolkit's control in VS Designer?

I'm used to create my forms with the designer, and modifying the XAML manually only when needed.
Even though the extended toolkit is a fantastic library, it still lacks integration with Visual Studio's designer. Can, by any mean, the toolkit's controls be displayed in the designer (not the toolbox), like standard controls? And if not, is there an explanation?
For now, the toolkit's controls are just blank and unselectable with simple clicks.
NOTE: It seems that the issue happens with container components (like BusyIndicator and Wizard/WizardPage) only.
EDIT: Here's some of my XAML. With this, I can see the wizard's first page, but no obvious way to see the others. If I put my Wizard in a BusyIndicator, can't see a thing at all.
<Window x:Class="MyProgram.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProgram"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="My Program" Height="477" Width="688" MinWidth="688" MinHeight="478">
<xctk:Wizard x:Name="wizard" FinishButtonClosesWindow="True" Next="wizard_Next">
<xctk:WizardPage Name="Intro_Page" Title="ABC" Description="abc" NextPage="{Binding ElementName=Second_Page}" Enter="disallowNext">
<xctk:WizardPage.Content>
<Grid>
<Label Content="abc" HorizontalAlignment="Center" Margin="0,54,0,87" Name="intro_lbl" VerticalAlignment="Center" Grid.Column="1" Padding="5" HorizontalContentAlignment="Center" Height="28" IsEnabled="False" />
</Grid>
</xctk:WizardPage.Content>
</xctk:WizardPage>
<xctk:WizardPage Name="Second_Page" Title="DFG" Description="dfg" NextPage="{Binding ElementName=Last_Page}">
<xctk:WizardPage.Content>
<Grid Name="grid">
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Stretch" Name="txt_second" VerticalAlignment="Center" />
</Grid>
</xctk:WizardPage.Content>
</xctk:WizardPage>
<xctk:WizardPage Name="Last_Page" PageType="Interior"
Title="Last Page"
Description="This is the last page in the process"
CanFinish="True" />
</xctk:Wizard>
</Window>
I know this answer comes late, but yesterday I was stumbling myself over this problem and maybe it is useful for others who land here.
I solved it by creating an own control derived from WizardPage.
Just Create a new UserControl, include the Xceed.Wpf.Toolkit namespace and change the Type from UserControl to xctk:WizardPage in the xaml file as well as in the code-behind file.
<xctk:WizardPage x:Class="WizardTest.MyWizardPage"
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:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
xmlns:local="clr-namespace:WizardTest"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock>Hello World!</TextBlock>
</Grid>
</xctk:WizardPage>
Now you can create your UI using the VisualStudio Designer.
To place your page in the wizard, simply put an object of you derived page into its items:
<xctk:Wizard>
<local:MyWizardPage Title="My Wizard Page"/>
</xctk:Wizard>
You can use Bindungs in your costum page out of the box because DataContext of the MyWizardPage object is set automatically to the wizards DataContext (which inherits from the Window DataContext if not explicitly given)

MVVM - Access command from different class in XAML

In my project I have a TitleView and GameView. When the program launches, TitleView is displayed. The user clicks a button and GameView is displayed. I am using MVVM Light which includes MainViewModel which has commands to switch to the desired views:
From MainViewModel.cs
GameViewCommand = new RelayCommand(() => ExecuteGameViewCommand());
private void ExecuteGameViewCommand()
{
CurrentViewModel = MainViewModel._gameViewModel;
}
In TitleView.xaml, I need to access this command and I don't know how. I am very much a novice when it comes to XAML.
From TitleView.xaml
<UserControl x:Class="AoW.Views.TitleView"
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:AoW.ViewModels"
mc:Ignorable="d"
d:DesignWidth="1020"
d:DesignHeight="740"
Width="1020"
Height="740">
<Button Content="New Game"
//This needs to bind to GameViewCommand in MainViewModel.cs
Command="{Binding GameViewCommand}"
Grid.Column="1"
Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
If I put the following line into TitleView.xaml...
DataContext="{Binding Main, Source={StaticResource Locator}}"
... I can access GameViewCommand, but I can't access any local commands.
What can I do to gain access to GameViewCommand while maintaining control of local commands?
If providing additional code would be helpful, please let me know.
I've fixed the problem. All I had to was this:
<Button Content="New Game"
Command="{Binding GameViewCommand}"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Grid.Column="1"
Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Button Content="Say Hello"
Command="{Binding SayHelloCommand}"
Grid.Column="2"
Grid.Row="2"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
Turns out it was a very easy fix.

Centre Button text

I have a user control with some Buttons. Each Button contains text which I'm struggling to align/centre within the control.
The XAML I have is
<UserControl x:Class="ProjectX.DetailedInfo"
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="28" d:DesignWidth="575">
<Grid Height="28" Width="575">
<Button Height="24" HorizontalAlignment="Left" Margin="525,2,0,0" Name="buttonOP1" VerticalAlignment="Top" Width="46" Click="buttonOP1_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="11" />
</Grid>
</UserControl>
But the text is not aligning properly, see below
HorizontalContentAlignment="Center" and VerticalContentAlignment="Center" doesn't seem to be working as expected. Does anyone know why?
(I've checked the text and there's no extra characters)
Thank you
Button content is by default centered in WPF. I would assume that for the buttons that have mis-aligned captions, the text contains spaces after the actual number. But it's hard to tell without you posting all relevant code.

issue in watermark textbox

I am new to wpf.I downloaded the wpf toolkit and then added it's dll file into my project file.Then tried some code to create watermark textbox but showing error(The tag 'WatermarkTextBox' does not exist in XML namespace).
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
x:Class="TRAINING.WaterMark"
x:Name="Window"
Title="WaterMark"
Width="640" Height="480">
<extToolkit:WatermarkTextBox>
<extToolkit:WatermarkTextBox.Watermark>
<StackPanel Orientation="Horizontal">
<Image Source="Contact16.png" Stretch="None" />
<TextBlock Text="Enter First Name" Margin="4,0,0,0" />
</StackPanel>
</extToolkit:WatermarkTextBox.Watermark>
</extToolkit:WatermarkTextBox>
</Window>
Please help.
I have not used the wpf toolkit before, but I'm pretty sure that as you have added the namespace using xctk, then you should use that in the xaml - i.e. xctk:WatermarkTextBox instead of extToolkit:WatermarkTextbox
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
x:Class="TRAINING.WaterMark"
x:Name="Window"
Title="WaterMark"
Width="640" Height="480">
<extToolkit:WatermarkTextBox>
<extToolkit:WatermarkTextBox.Watermark>
<StackPanel Orientation="Horizontal">
<Image Source="Contact16.png" Stretch="None" />
<TextBlock Text="Enter First Name" Margin="4,0,0,0" />
</StackPanel>
</extToolkit:WatermarkTextBox.Watermark>
</extToolkit:WatermarkTextBox>
</Window>
I Guess You Missed the refrence of that extToolkit so i added. i hope it will help you
My reason for this issue was confusion between xceed ($) vs dotnetprojects (Free) NuGet package.
So once I uninstalled the xceed, everything was working fine and error was resolved.

Categories