WPF: How to make binding of IsOpen property in a professional way?
up vote
-1
down vote
favorite
I need to process with IsOpen property in a code Behind.
XAML:
DataContext="Binding RelativeSource=RelativeSource Self"
IsOpen="Binding ChildWindow_IsOpen"
Code Behind:
public bool ChildWindow_IsOpen
get return (bool)GetValue(WindowProperty);
set SetValue(WindowProperty, value);
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register("ChildWindow_IsOpen", typeof(bool), typeof(MainWindow));
MainWindow:
ChildWindow childWindow = new ChildWindow();
private async void button3_OnClick(object sender, RoutedEventArgs e)
if (childWindow.ChildWindow_IsOpen == false)
await this.ShowChildWindowAsync(new ChildWindow() IsModal = false, AllowMove = true, , RootGrid);
childWindow.ChildWindow_IsOpen = true;
else if (childWindow.ChildWindow_IsOpen == true)
childWindow.Close();
childWindow.ChildWindow_IsOpen = false;
else
return;
So my question is how to do that in a professional way?
My code doesn't affect to ChildWindow at all.
Thanks in advance!
Update: ChildWindow's XAML is situated in MainWindow XAML. This works like a charm!
private void button1_Click(object sender, RoutedEventArgs e)
{
if (ChildWindow.IsOpen == false)
ChildWindow.IsOpen = true;
else if (ChildWindow.IsOpen == true)
ChildWindow.Close();
else
return;
c# wpf xaml binding
|
show 3 more comments
up vote
-1
down vote
favorite
I need to process with IsOpen property in a code Behind.
XAML:
DataContext="Binding RelativeSource=RelativeSource Self"
IsOpen="Binding ChildWindow_IsOpen"
Code Behind:
public bool ChildWindow_IsOpen
get return (bool)GetValue(WindowProperty);
set SetValue(WindowProperty, value);
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register("ChildWindow_IsOpen", typeof(bool), typeof(MainWindow));
MainWindow:
ChildWindow childWindow = new ChildWindow();
private async void button3_OnClick(object sender, RoutedEventArgs e)
if (childWindow.ChildWindow_IsOpen == false)
await this.ShowChildWindowAsync(new ChildWindow() IsModal = false, AllowMove = true, , RootGrid);
childWindow.ChildWindow_IsOpen = true;
else if (childWindow.ChildWindow_IsOpen == true)
childWindow.Close();
childWindow.ChildWindow_IsOpen = false;
else
return;
So my question is how to do that in a professional way?
My code doesn't affect to ChildWindow at all.
Thanks in advance!
Update: ChildWindow's XAML is situated in MainWindow XAML. This works like a charm!
private void button1_Click(object sender, RoutedEventArgs e)
{
if (ChildWindow.IsOpen == false)
ChildWindow.IsOpen = true;
else if (ChildWindow.IsOpen == true)
ChildWindow.Close();
else
return;
c# wpf xaml binding
What do you mean by "professional"? What do you mean "My code doesn't affect to ChildWindow"? You seem to be doingchildWindow.ChildWindow_IsOpen
just fine
– MickyD
Nov 10 at 14:09
2
The “professional” way would be to use MVVM
– Dave M
Nov 10 at 14:13
@DaveM Exactly. WPF/UWP and XAML were designed with MVVM in mind. While you can use any other approach and pattern, doing so missed about 90% of it's power and runs into issues at every other corner. This does not look like proper MVVM, so I asume the issue is there.
– Christopher
Nov 10 at 14:17
@MickyD, first button click should open ChildWindow, second one should close ChildWindow. However it doesn't happen.
– Pew
Nov 10 at 14:17
@Christopher Since the OP wants to open/close windows, MVVM has questionable benefit for this particular problem. If you intend to open a view from the viewmodel (including by indirect means of injected window'ing services into the VM) then that is a violation of the MVVM best practices
– MickyD
Nov 10 at 14:45
|
show 3 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I need to process with IsOpen property in a code Behind.
XAML:
DataContext="Binding RelativeSource=RelativeSource Self"
IsOpen="Binding ChildWindow_IsOpen"
Code Behind:
public bool ChildWindow_IsOpen
get return (bool)GetValue(WindowProperty);
set SetValue(WindowProperty, value);
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register("ChildWindow_IsOpen", typeof(bool), typeof(MainWindow));
MainWindow:
ChildWindow childWindow = new ChildWindow();
private async void button3_OnClick(object sender, RoutedEventArgs e)
if (childWindow.ChildWindow_IsOpen == false)
await this.ShowChildWindowAsync(new ChildWindow() IsModal = false, AllowMove = true, , RootGrid);
childWindow.ChildWindow_IsOpen = true;
else if (childWindow.ChildWindow_IsOpen == true)
childWindow.Close();
childWindow.ChildWindow_IsOpen = false;
else
return;
So my question is how to do that in a professional way?
My code doesn't affect to ChildWindow at all.
Thanks in advance!
Update: ChildWindow's XAML is situated in MainWindow XAML. This works like a charm!
private void button1_Click(object sender, RoutedEventArgs e)
{
if (ChildWindow.IsOpen == false)
ChildWindow.IsOpen = true;
else if (ChildWindow.IsOpen == true)
ChildWindow.Close();
else
return;
c# wpf xaml binding
I need to process with IsOpen property in a code Behind.
XAML:
DataContext="Binding RelativeSource=RelativeSource Self"
IsOpen="Binding ChildWindow_IsOpen"
Code Behind:
public bool ChildWindow_IsOpen
get return (bool)GetValue(WindowProperty);
set SetValue(WindowProperty, value);
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register("ChildWindow_IsOpen", typeof(bool), typeof(MainWindow));
MainWindow:
ChildWindow childWindow = new ChildWindow();
private async void button3_OnClick(object sender, RoutedEventArgs e)
if (childWindow.ChildWindow_IsOpen == false)
await this.ShowChildWindowAsync(new ChildWindow() IsModal = false, AllowMove = true, , RootGrid);
childWindow.ChildWindow_IsOpen = true;
else if (childWindow.ChildWindow_IsOpen == true)
childWindow.Close();
childWindow.ChildWindow_IsOpen = false;
else
return;
So my question is how to do that in a professional way?
My code doesn't affect to ChildWindow at all.
Thanks in advance!
Update: ChildWindow's XAML is situated in MainWindow XAML. This works like a charm!
private void button1_Click(object sender, RoutedEventArgs e)
{
if (ChildWindow.IsOpen == false)
ChildWindow.IsOpen = true;
else if (ChildWindow.IsOpen == true)
ChildWindow.Close();
else
return;
c# wpf xaml binding
c# wpf xaml binding
edited Nov 10 at 15:05
asked Nov 10 at 13:51
Pew
12
12
What do you mean by "professional"? What do you mean "My code doesn't affect to ChildWindow"? You seem to be doingchildWindow.ChildWindow_IsOpen
just fine
– MickyD
Nov 10 at 14:09
2
The “professional” way would be to use MVVM
– Dave M
Nov 10 at 14:13
@DaveM Exactly. WPF/UWP and XAML were designed with MVVM in mind. While you can use any other approach and pattern, doing so missed about 90% of it's power and runs into issues at every other corner. This does not look like proper MVVM, so I asume the issue is there.
– Christopher
Nov 10 at 14:17
@MickyD, first button click should open ChildWindow, second one should close ChildWindow. However it doesn't happen.
– Pew
Nov 10 at 14:17
@Christopher Since the OP wants to open/close windows, MVVM has questionable benefit for this particular problem. If you intend to open a view from the viewmodel (including by indirect means of injected window'ing services into the VM) then that is a violation of the MVVM best practices
– MickyD
Nov 10 at 14:45
|
show 3 more comments
What do you mean by "professional"? What do you mean "My code doesn't affect to ChildWindow"? You seem to be doingchildWindow.ChildWindow_IsOpen
just fine
– MickyD
Nov 10 at 14:09
2
The “professional” way would be to use MVVM
– Dave M
Nov 10 at 14:13
@DaveM Exactly. WPF/UWP and XAML were designed with MVVM in mind. While you can use any other approach and pattern, doing so missed about 90% of it's power and runs into issues at every other corner. This does not look like proper MVVM, so I asume the issue is there.
– Christopher
Nov 10 at 14:17
@MickyD, first button click should open ChildWindow, second one should close ChildWindow. However it doesn't happen.
– Pew
Nov 10 at 14:17
@Christopher Since the OP wants to open/close windows, MVVM has questionable benefit for this particular problem. If you intend to open a view from the viewmodel (including by indirect means of injected window'ing services into the VM) then that is a violation of the MVVM best practices
– MickyD
Nov 10 at 14:45
What do you mean by "professional"? What do you mean "My code doesn't affect to ChildWindow"? You seem to be doing
childWindow.ChildWindow_IsOpen
just fine– MickyD
Nov 10 at 14:09
What do you mean by "professional"? What do you mean "My code doesn't affect to ChildWindow"? You seem to be doing
childWindow.ChildWindow_IsOpen
just fine– MickyD
Nov 10 at 14:09
2
2
The “professional” way would be to use MVVM
– Dave M
Nov 10 at 14:13
The “professional” way would be to use MVVM
– Dave M
Nov 10 at 14:13
@DaveM Exactly. WPF/UWP and XAML were designed with MVVM in mind. While you can use any other approach and pattern, doing so missed about 90% of it's power and runs into issues at every other corner. This does not look like proper MVVM, so I asume the issue is there.
– Christopher
Nov 10 at 14:17
@DaveM Exactly. WPF/UWP and XAML were designed with MVVM in mind. While you can use any other approach and pattern, doing so missed about 90% of it's power and runs into issues at every other corner. This does not look like proper MVVM, so I asume the issue is there.
– Christopher
Nov 10 at 14:17
@MickyD, first button click should open ChildWindow, second one should close ChildWindow. However it doesn't happen.
– Pew
Nov 10 at 14:17
@MickyD, first button click should open ChildWindow, second one should close ChildWindow. However it doesn't happen.
– Pew
Nov 10 at 14:17
@Christopher Since the OP wants to open/close windows, MVVM has questionable benefit for this particular problem. If you intend to open a view from the viewmodel (including by indirect means of injected window'ing services into the VM) then that is a violation of the MVVM best practices
– MickyD
Nov 10 at 14:45
@Christopher Since the OP wants to open/close windows, MVVM has questionable benefit for this particular problem. If you intend to open a view from the viewmodel (including by indirect means of injected window'ing services into the VM) then that is a violation of the MVVM best practices
– MickyD
Nov 10 at 14:45
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239635%2fwpf-how-to-make-binding-of-isopen-property-in-a-professional-way%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "professional"? What do you mean "My code doesn't affect to ChildWindow"? You seem to be doing
childWindow.ChildWindow_IsOpen
just fine– MickyD
Nov 10 at 14:09
2
The “professional” way would be to use MVVM
– Dave M
Nov 10 at 14:13
@DaveM Exactly. WPF/UWP and XAML were designed with MVVM in mind. While you can use any other approach and pattern, doing so missed about 90% of it's power and runs into issues at every other corner. This does not look like proper MVVM, so I asume the issue is there.
– Christopher
Nov 10 at 14:17
@MickyD, first button click should open ChildWindow, second one should close ChildWindow. However it doesn't happen.
– Pew
Nov 10 at 14:17
@Christopher Since the OP wants to open/close windows, MVVM has questionable benefit for this particular problem. If you intend to open a view from the viewmodel (including by indirect means of injected window'ing services into the VM) then that is a violation of the MVVM best practices
– MickyD
Nov 10 at 14:45