Catel + Orchestra MahApps - Property 'Owner' is not found on the object - mahapps.metro

I'm using Catel + Orchestra.MahApps to build my application.
I'm trying to get custom Dialog working, and right now I'm stuck.
I have created SimpleDataWindow:
<orchestra:SimpleDataWindow x:Class="OrtMan.ViewModule.Photos.Views.NewPhotoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:catel="http://catel.codeplex.com"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:orchestra="https://github.com/orcomp/orchestra"
xmlns:viewModels="clr-namespace:OrtMan.ViewModule.Photos.ViewModels"
d:DataContext="{d:DesignInstance viewModels:NewPhotoWindowViewModel}"
mc:Ignorable="d">
<Grid>
<TextBlock Text="aaa" />
</Grid>
</orchestra:SimpleDataWindow>
Then I'm trying to show it from my ViewModel like this:
var uiservice = ServiceLocator.Default.ResolveType<IUIVisualizerService>();
var showDialog = uiservice.ShowDialog<NewPhotoWindowViewModel>();
if (showDialog == true)
{
...
}
The result is that:
- Error in Catel logs: Property 'Owner' is not found on the object 'NewPhotoWindow', probably the wrong field is being mapped
- Dialog shows but without content
- it automatically returns true as dialogResult
I have tried with multiple IUIVisualizerService methods: Show/ShowDialog and also with async. Always same problem.
Am I doing something wrong?

If I remember correctly, all the dialogs inside MahApps.Metro are async. This means you should use the async methods of the IUIVisualizerService to show the dialogs.

Related

WPF Binding DataContext in class

I'm doing something similar to this : https://www.wpf-tutorial.com/data-binding/using-the-datacontext/.I write this.DataContext = this;
in the cs file after the InitialieComponents(), and in the cs.xaml file I write Command = "{Binding BackCommand}" for a button.
But the resharper gives me an error: Cannot resolve symbol ”BackCommand“ due to unknown DataContext and the binding does not work.
Only If I do something like this it works: Command = "{Binding BackCommand, elementName = root}" and set the Name of the root xml(My class) to "root".
I dont understand why and how to fix this.. (Since I have seen instances where it worked just fine like in the url)
Thank you
You could add a DesignerDataContext to your XAML like:
<Window ...
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:MyWindow, IsDesignTimeCreatable=False}"
>
This makes tools aware of the type of the DataContext.
Side note: I would recommend using the MVVM pattern and using a separate ViewModel class as DataContext.

Left-click doesn't work on web canvas being hosted in WPF WebBrowser

I'm trying to present vis.js web page in my WPF using WebBrowser, the page presented successfully but I can left-click any of the items inside the canvas.
Mouse zoom is working as expected inside the canvas.
I wrote a simple project contains only 2 files:
MainWindow.xaml:
<Window x:Class="WpfApplication1.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">
<Grid>
<WebBrowser x:Name="webBrowser"></WebBrowser>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
webBrowser.Navigate("http://visjs.org/examples/network/other/configuration.html");
}
}
}
Note: I added my registry the following key so my WPF process will use IE11:
Key name: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\processName.exe
Type: REG_DWORD Value Data: 11000 (decimal)
I won't answer your question directly but I'll give you some advice based on my experience, and that is: never use default wpf WebBrowser for anything even remotely serious. I tried to use it before many times for many different projects and always got some showstopper issue, no exceptions. And of course that is with IE11 (10,9 - I tried it many times as I said) emulation.
Look at your case for example. Not only canvas clicks do not work - whole page is completely destroyed. Sliders are messed up, if you try to choose a color - clicks again do not work, and many more issues on this single page.
So, don't bother with wpf WebBrowser and just use real browser, for example Chromium. I personally prefer CefSharp which has wpf binding to chromium. Just install their nuget package (CefSharp.WPF), then:
<Window x:Class="WpfApplication2.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:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<wpf:ChromiumWebBrowser x:Name="webBrowser"/>
</Grid>
</Window>
And
InitializeComponent();
webBrowser.Address = "http://visjs.org/examples/network/other/configuration.html";
And you page just displays as it should. Canvas clicks work, no styles are destoyed, color pickers work. In addition to all that, cefsharp browser control is much more flexible. You can call to\from javascript easily, intercept almost every event (resource load, redirections), you can disable javascript\images, even intercept requests and feed completely different data. And no need to fix registry on every client machine.
Note that if you would install CefSharp.WPF via package, it will require your project to have x86\x64 platform (does not work with AnyCPU). But you really can make it work with anyCPU with little effort, if you would have such requirement.

click on hyperlink (navigateuri) in one view should open another view in WPF MVVM

I am working on a WPF project which implements the MVVM architecture. I have a requirement where, a click on a hyperlink in one view(ux){Usercontrol} should open another view(window). I am able to load a view(usercontrol) through another view(window) by simply mentioning namespace:UsercontrolViewName in the host view, but I am clueless about how to do it through a hyperlink.
<UserControl x:Class="CCSAdvantage.AddOn.UX.DisplayPhoneNumbersView"
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:res="clr-namespace:CCSAdvantage.AddOn.UX.Internationalization"
xmlns:local="clr-namespace:CCSAdvantage.AddOn.UX"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d>
<Grid>
<TextBlock Grid.Row="0">
<HyperlinkNavigateUri="">AdditionalPhone(s)/Manage</Hyperlink>
</TextBlock>
</Grid>
</UserControl>
This is the code for my UserControl, the Grid has other elements but I eliminated the details for simplicity. Additional Phone(s)/Manage => On clicking this link, a new window view which is also in xaml should open up. I tried giving the Window views name in the navigateuri property, but it does not work
Any help is appreciated. Thanks!
My problem was accessing the another view(v2) from the current view(v1), I am in. This can be achieved by using a delegate which is called at runtime in the v1's viewmodel, which calls window loader function in V2, this would load the window view(v2)
Check this Hyperlink in WPF Application.

User control that opens dialog, where dialog result updates properties in viewmodel of the page that uses it

I have been struggling with this question for some time now. It seems relatively simple, but I am unsure of how to solve it. Would love your aid on this.
I have a page that's bound to viewmodel, that has some string properties that represent file paths.
I've built a user control that has a textbox and a button. The control's purpose: show a dialog when you click on the button(file dialog), and update the textbox according to the result.
Here is the xaml:
<UserControl x:Class="MyProject.Controls.FilePickerLauncherControl"
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"
x:Name="me"
d:DesignHeight="300" d:DesignWidth="300">
<DockPanel>
<Button Content="..." DockPanel.Dock="Right" Command="{Binding OpenFileDialogCommand,ElementName=me}"></Button>
<TextBox Text="{Binding Path=PathChosen,ElementName=me}" DockPanel.Dock="Right" VerticalAlignment="Center" Height="30"></TextBox>
</DockPanel>
This is my the command in page's view model(the action):
private void OpenFileExplorer()
{
SimpleIoc.Default.GetInstance<IInputOutputService>().OpenFileDialog(modelsDirectory);
}
Now what I want to do, for every instance of my control, to take the result of the dialog and put it in the textbox that's in the control. I've been thinking of:
maybe a command parameter that would be sent to this OpenFileExplorer, telling which property should be updated somehow, but I am not sure how.
Write this command multiple times for each property. I don't think this is correct.
Create a new viewmodel for my control with this command and make instances of it in my current view model.
What would you guys say the best way to do this? it is relatively simple, although it got me kind of confused.

Placing Custom WPF Control inside of a StackPanel

I'm trying to create a custom WPF control and place it inside of a StackPanel in my XAML file. I originally made a custom UserControl and got the following error message:
A value of type 'CustomControl' cannot be added to a collection or dictionary of type 'UIElementCollection'.
I tried changing my custom control from a UserControl to a UIElement, but I still get this message. How do I make a custom control that I can place inside of the StackPanel?
Restart Visual Studio. The XAML designer is a notoriously sensitive beast.
How are you creating the CustomControl? Make sure it is inheriting from UserControl.
I just created a new project called "TestProj" - right clicked in the solution explorer Add=>UserControl and named it CustomControl.
I was able to insert it via the following code:
<Window x:Class="TestProj.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestProj"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<local:CustomControl/>
</StackPanel>
</Grid>
This could also happen if you're subclassing another control class, but your .xaml.cs file is still subclassing UserControl.

Resources