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;

}









share|improve this question
























  • 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

















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;

}









share|improve this question
























  • 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















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;

}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ