Cannot select objects in XAML Designer when using dragablz and MahApps - c#

I am developing an application with the use of dragablz, MahApps and MaterialDesign. However, I have broken down the problem to the dragablz and MahApps extension.
I create two tab items (extract from demo code):
<controls:MetroWindow x:Class="MahMaterialDragablzMashUp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz"
xmlns:mahMaterialDragablzMashUp="clr-namespace:MahMaterialDragablzMashUp"
xmlns:dockablz="clr-namespace:Dragablz.Dockablz;assembly=Dragablz"
xmlns:wpf="http://materialdesigninxaml.net/winfx/xaml/themes"
WindowTransitionsEnabled="False"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
Background="{DynamicResource MaterialDesignPaper}"
GlowBrush="{DynamicResource AccentColorBrush}"
FontFamily="{StaticResource DefaultFont}"
Title="Material-MahApps-Dragablz Mash Up" Height="640" Width="800">
<dockablz:Layout>
<dragablz:TabablzControl BorderThickness="0"
Margin="0,-1,0,1">
<dragablz:TabablzControl.InterTabController>
<dragablz:InterTabController />
</dragablz:TabablzControl.InterTabController>
<TabItem Header="DIALOGS">
<mahMaterialDragablzMashUp:Mah Margin="16" />
</TabItem>
<TabItem Header="MAHAPPS">
<mahMaterialDragablzMashUp:Mah Margin="16" />
</TabItem>
</dragablz:TabablzControl>
</dockablz:Layout>
</controls:MetroWindow>
The code works fine, but when I try to switch to another tab in the XAML designer by clicking on it, the whole section is selected but not any single object, so I cannot click on another tab:
The view in the designer changes when I click into the corresponding code lines in the XAML editor, but not when I click on the object in the designer. This makes simple design steps (like moving buttons around) very difficult as I cannot click and drag them.
Up to now, I have tried the following:
using Visual Studio 2013 Professional
using Visual Studio 2015 Community
Build the project
restart VS and the computer
I found the following question and the provided solution by 'JFTxJ' (that empty Grid elements are causing the problem) is not working for me, as I do not have any Grid items.
Same for the answer from 'Moon Waxing' (to add the attribute Visibility="Collapsed" to the tabiItem) is not working for me.
Other StackOverflow question: Cannot click or select Control XAML design
Is this a common problem or is there any solution to it?

Related

VS WPF Designer: how to add control from referenced project to a form in the main project

I have a very simple two-project solution in Visual Studio 2015 Community. One project is a WPF application; the other is a class library. I have added a project reference in the main project to the class library. The class library contains one WPF UserControl named TestControl with the following XAML:
<UserControl x:Class="SomeControls.TestControl"
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:SomeControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="38,29,0,0" VerticalAlignment="Top" Width="75"/>
<CheckBox x:Name="checkBox" Content="CheckBox" HorizontalAlignment="Left" Margin="163,40,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="83,102,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="121" Width="164"/>
</Grid>
</UserControl>
The only controls it contains are standard WPF controls.
The class library project builds successfully, and when I open the WPF designer for the main window in the WPF Application project, the TestControl control from the class library appears in the Toolbox. But when I select that control from the Toolbox and try to add an instance to my main window, I get a dialog that states:
The following reference has been added to the project:
"SomeControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
In order to use types from the new reference, press OK to restart the
XAML Designer. After the XAML Designer restarts, re-create the
control on the artboard.
When I click "OK," nothing changes, and if I try to add the control again, I get the same error dialog. I have tried closing and re-opening the XAML file as well as closing and re-opening Visual Studio.
Why won't Visual Studio let me add a control from a referenced project via the designer?
Here is a screenshot showing my setup and the dialog I get:
Clear everything related with custom control currently. Drag the DLL directly to the Toolbox, no need to add the reference seperately. This dragging operation makes everything you need. It adds the DLL as reference, the xmlns namespace and the assembly name automatically.
I tested it using your code and it works.
Gmap.net Error in WPF
Clear everything related with your DLL in reference and toolbox. Close the VS and then :
Find your dll file (via windows explorer or etc)
Right click on it
Select properties
Click the unblock button and apply and ok.
1.Select "SomeControls" in "Reference, and set "Copy Local" to true.
2.Then build your "wpfApplication1"

How can I display the current day of the week in XAML using the current regional settings?

I have the following XAML:
<Window x:Class="String_Format.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:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="250">
<StackPanel Margin="10">
<TextBlock Name="TextBlock" Text="{Binding Source={x:Static s:DateTime.Now}, StringFormat=Date: {0:dddd, MMMM dd}}"/>
</StackPanel>
</Window>
In the XAML designer in Visual Studio 2015 as well as in the running application, this displays as: "Date: Thursday, January 28". As I am on a German Windows 7 with regional settings set to "German (Germany)", I had actually expected: "Date: Donnerstag, Januar 28".
So I went and added the namespace
xmlns:c="clr-namespace:System.Globalization;assembly=mscorlib"
and altered my TextBlock to:
<TextBlock Name="TextBlock" Text="{Binding Source={x:Static s:DateTime.Now}, ConverterCulture={x:Static c:CultureInfo.CurrentCulture}, StringFormat=Date: {0:dddd, MMMM dd}}"/>
This indeed results in my desired behavior at runtime, but in the XAML designer, it says "Invalid Markup", and there's no preview of my window displayed an more.
This has been asked before, but I wanted to do it inside XAML, without using code behind, if possible.
So what's wrong with my XAML? I'd like to understand.
This indeed results in my desired behavior at runtime, but in the XAML designer, it says "Invalid Markup", and there's no preview of my window displayed an more.
This is because the added code: ConverterCulture={x:Static c:CultureInfo.CurrentCulture} cannot be resolved at design time. This results in the invalid markup and lack of display. It is resolved at runtime, which is why your date displays in the correct language/culture.
You can only solve this by moving this into code where you can check whether it's runtime or design time.
Checkout if setting the Language on the Window helps, I do this in the code behind of a window / usercontrol etc:
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

MVVM Binding not working in design-time

I tried to bind the visibility of a few buttons to some booleans in a viewmodel and make it work in design-time. I did this several times and never had a problem, but now it does not work and I don't have a clue why. Note that everything works
fine when I run the application.
I extracted the essence into a separate app and it still doesn't work!
My XAML:
<Window x:Class="BindingTest.MainWindow"
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:bindingTest="clr-namespace:BindingTest"
Title="MainWindow" Height="350" Width="525"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance bindingTest:TestViewModel, d:IsDesignTimeCreatable=true}">
<Grid>
<Grid.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Grid.Resources>
<StackPanel>
<Button Visibility="{Binding IsButton1Visible, Converter={StaticResource BoolToVis}}">Hallo 1</Button>
<Button Visibility="{Binding Button2Visibility}">Hallo 2</Button>
<Button>Hallo 3</Button>
</StackPanel>
</Grid>
</Window>
My ViewModel:
public class TestViewModel
{
public bool IsButton1Visible
{
get { return true; }
}
public Visibility Button2Visibility
{
get { return Visibility.Hidden; }
}
}
To make it work in the real app, i added to the constructor of the XAML:
DataContext = new TestViewModel();
This is all pretty simple, but why is it not working in the designer?? I always got this to work in the past and now I tried for hours... It's not the only binding not working, but why is not even this working??
Expected Result:
Button1 visible
Button2 hidden
Button3 visible
Result in the designer:
Button1 collapsed
Button2 visible
Button3 visible
UPDATE: The problem with design-time DataContext bindings not working is due to mistakenly using the d: namespace prefix for the IsDesignTimeCreatable property.
So, change
d:IsDesignTimeCreatable=True
to
IsDesignTimeCreatable=True
and everything should be fine. (My bug report filed with MS will be updated with this information as well.)
As this problem with the design-time DataContext bindings not working was consistently reproducable in VS2015 as well as VS2013 when IsDesignTimeCreatable is prefixed with the d: namespace (see comments below the question), i decided to file a bug report with Microsoft: Issue #1651633 "WPF designer: Designtime DataContext bindings broken?".
Lets see how Microsofts will follow-up with regard to this issue. It should be addressed, since such an issue can catch a developer off-guard and make him reinstall Visual Studio for no benefit... ;)
If anything is OK with the code, there could be an other tricky problem:
In the Designer of Visual Studio a new button was introduced in 2019, which enables / disables the preview of design data.
It's a very tiny button on botton left of the designer window. It's tooltip is "Enable Project Code".
Click this important toggle button to see the design data. (But be aware that you don't disable it, if it's already enabled.)

Customise WinRT XAML Toolkit - Calendar Control

I wrote a Windows Phone 8.1 (WINRT) App. I need to show Calendar in the page. So, I added WinRT XAML Toolkit - Calendar Control from nuget.
PM> Install-Package WinRTXamlToolkit.Controls.Calendar
<Page
x:Class="DrFit.Pages.ActivityTimeTablePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DrFit.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:WinRT="using:WinRTXamlToolkit.Controls"
Background="Black">
<Grid x:Name="LayoutRoot">
<WinRT:Calendar Height="500">
</WinRT:Calendar>
</Grid>
</Page>
How to Customise this Calendar control, example FontWeight,Foreground,Background?
If the properties aren't exposed by the control or template-bound to template part properties - you'll probably need to change the template. You can find the default template here and templates for calendar parts are in the same folder:
WinRTXamlToolkit.Controls.Calendar/WinRTXamlToolkit.Controls.Calendar.Shared/Controls/Calendar
You probably have figured out the answer to this but I was facing a similar problem and figured out a solution so I thought I'd post it here for anyone else who faces a similar conundrum. If the calendar control is going out of the page, you can wrap it inside a Viewbox so that it fits the screen. This is how I managed to do it.
<Viewbox Stretch="Uniform">
<Grid Height="600" Width="600">
<toolkit:Calendar HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="Calendar_Tapped"/>
</Grid>
</Viewbox>

Add Windows User Control to a WPF View

I need to add a Windows form User Control (Barcode_Scanner.cs) to a WPF View (MainWindow.xaml)
Is there a simple way to do this?
Any help would be appreciated.
You can host Windows.Forms controls using the WPF WindowsFormsHost element.
Example:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:myControls="clr-namespace:MyContromNamespace;assembly=MyContromNamespace"
Title="MainWindow" Height="195" Width="191" Name="UI">
<Grid>
<WindowsFormsHost>
<winforms:Button Text="StackOverflow" />
</WindowsFormsHost>
<WindowsFormsHost>
<myControls:MyUserControl />
</WindowsFormsHost>
</Grid>
</Window>
Result:
you can do it with WindowsFormsHost
So the first thing that we need to do (after creating a new WPF project) is add a few references. You can do this by by right-clicking on the references folder in the solution explorer, and choosing "Add Reference":
Then you will get a dialog like this:
and so on wpf-tutorial-using-winforms-in-wpf

Categories