This is a migrated thread and some comments may be shown as answers.

How can I center my RadWindow when binding the WindowStartupLocation with its value in the ViewModel?

8 Answers 136 Views
Window
This is a migrated thread and some comments may be shown as answers.
Laurent
Top achievements
Rank 1
Laurent asked on 02 Oct 2020, 05:21 AM

I have defined a RadWindow like this:

01.<telerik:RadWindow x:Class="Shell.ShellView"
02.                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
03.                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
04.                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
05.                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
06.                   xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
07.                   xmlns:shell="clr-namespace:Shell"
08.                   mc:Ignorable="d"
09.                   d:DesignHeight="450" d:DesignWidth="800"
10.                   d:DataContext="{d:DesignInstance shell:ShellViewModel}"
11.                   Width="800"
12.                   Height="475"
13.                   Header="{Binding Header}"
14.                   WindowStartupLocation="{Binding StartupLocation}"
15.                   navigation:RadWindowInteropHelper.ShowInTaskbar="{Binding ShowInTaskBar}">
16.     [...]
17. 
18.</telerik:RadWindow>

 

Note the bindings on lines 13 - 15.

The corresponding view model looks like this:

01.public class ShellViewModel
02.{
03.    public bool ShowInTaskBar => true;
04. 
05.    public WindowStartupLocation StartupLocation => WindowStartupLocation.CenterScreen;
06. 
07.    public string Header => Resources.MainWindowHeader;
08. 
09.    [...]
10.}

 

As you can see, I am binding the Header, the StartupLocation, and the ShowInTaskBar properties. 

My problem is the following: while the bindings for the Header and ShowInTaskBar do work perfectly, the binding for StartupLocation doesn't. I understand why. 

Can someone tell me what I am missing here?

Thanks in advance!

 

 

8 Answers, 1 is accepted

Sort by
0
Laurent
Top achievements
Rank 1
answered on 02 Oct 2020, 05:26 AM
When I say that the binding for StartupLocation doesn't work, I mean that the window does not get centered. It gets displayed on the top left of the screen. If I hard-code the value in the xaml, it works. If I try to bind the StartupLocation to its value in the view model, as shown in the code above, and then run the application, and then debug it with visual studio's live tree, and then inspect the value of the WindowStartupLocation, I see it takes the view model's value. However, the window is not centered.
0
Dilyan Traykov
Telerik team
answered on 06 Oct 2020, 03:20 PM

Hello Laurent,

Thank you for the provided code snippets.

Although the WindowStartupLocation is defined as a dependency property, the actual binding cannot be evaluated before the window is shown and that is why it appears as if it is not respected. You can notice that the native Window control does not allow binding to its WindowStartupLocation property at all.

We will try to think of a possible solution for this scenario, but in the meantime, you can manually transfer the value from the viewmodel to the window before showing the window:

            var window = new ShellView();
            var viewModel = new ShellViewModel();
            window.DataContext = viewModel;
            window.WindowStartupLocation = viewModel.StartupLocation;
            window.Show();
Please let me know whether this would work for you.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Laurent
Top achievements
Rank 1
answered on 07 Oct 2020, 07:00 AM
Yes, it is fine, thanks. I would, however, prefer to be able to bind directly to the property in the view model, of course.
0
Dilyan Traykov
Telerik team
answered on 07 Oct 2020, 02:32 PM

Hello Laurent,

I'm happy to inform you that we've introduced a modification which should now allow you to bind the property as expected. It will be available with our next internal build on Monday (12.10).

Please let me know once you manage to test it out.

Regards,
Dilyan Traykov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

0
Laurent
Top achievements
Rank 1
answered on 12 Oct 2020, 06:57 AM
Sure, I'll give it a try. Thank you!!!
0
Accepted
Dilyan Traykov
Telerik team
answered on 12 Oct 2020, 11:43 AM

Hello Laurent,

The build should now be available for download in your Telerik account. Please inform me once you test it. 

Regards,
Dilyan Traykov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

0
Laurent
Top achievements
Rank 1
answered on 15 Oct 2020, 05:44 AM

Dear Dilyan,

I can gladly confirm that your bugfix works.

Thanks a lot for your work!

0
Dilyan Traykov
Telerik team
answered on 15 Oct 2020, 12:07 PM

Hi Laurent,

I'm happy to hear that the property now works as expected. Please let me know if you require any further assistance from my side.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Window
Asked by
Laurent
Top achievements
Rank 1
Answers by
Laurent
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or