How to make less code with multiple checkboxes that call the same command





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm currently doing a program where I want to have multiple checkboxes and multiple buttons where each button is bound to one checkbox. When the checkbox is enabled, it should hide the specific button.



For the moment I can do this for one button:



<CheckBox Name="cbxIsClosableForUser"
DataContext="{StaticResource GeneralVM}"
Command="{Binding BtnToggleLblVisibilityDelegateCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CommandParameter="{Binding LblVisibilityCloseButton}"
Content="{m:Translate ClosingAvailableForUser}"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"
Margin="6,0,0,0"
HorizontalAlignment="Left" VerticalAlignment="Center" />


On another View there's the buttons:



<Button DataContext="{StaticResource GeneralVM}"
Visibility="{Binding LblVisibilityCloseButton, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource NewButtonStyle}"
Grid.Column="3"
Grid.Row="3"
PreviewMouseDown="ImQuit_PreviewMouseDown"
HorizontalAlignment="Right"
VerticalAlignment="Bottom" >
<Image Name="imQuit"
Source="/CWI;component/Images/quit.png"
Stretch="None"/>
</Button>


Here's the code in the ViewModel:



    public Visibility LblVisibilityCloseButton
{
get => _LblVisibilityCloseButton;
set
{
OnPropertyChanged(nameof(LblVisibilityCloseButton));
_LblVisibilityCloseButton = value;
}
}
private Visibility _LblVisibilityCloseButton;

public GeneralViewModel()
{
LblVisibilityCloseButton = Visibility.Visible;
BtnToggleLblVisibilityDelegateCommand = new DelegateCommand<object>(ToggleVisibility);
}

public DelegateCommand<object> BtnToggleLblVisibilityDelegateCommand { get; set; }

private void ToggleVisibility(object obj)
{

if (LblVisibilityCloseButton == Visibility.Visible)
{
LblVisibilityCloseButton = Visibility.Hidden;
}
else
{
LblVisibilityCloseButton = Visibility.Visible;
}
}


Now the thing is that this checkbox works perfect. But I want multiple other checkboxes calling the same command without repeating myself and put 5 other if statements for 5 additionnal buttons.



Edit: I'm using MVVM, I don't want code behind stuff for every single checkbox.










share|improve this question

























  • Possible duplicate of How do I make a textbox visible and hidden with a checkbox?

    – ASh
    Nov 23 '18 at 12:21











  • No it's not. I'm using commands, I don't want any code behind to handle all the stuff. And the problem isn't at all around visiblity or not, it's about making code so I don't repeat myself. All the code above works fine.

    – Selim
    Nov 23 '18 at 12:25













  • I think that the idea here is to have multiple checkboxes, calling a same command to change the different button visibilities in the view model, in order to diminish the number of commands/functions

    – ygosteli
    Nov 23 '18 at 12:42


















1















I'm currently doing a program where I want to have multiple checkboxes and multiple buttons where each button is bound to one checkbox. When the checkbox is enabled, it should hide the specific button.



For the moment I can do this for one button:



<CheckBox Name="cbxIsClosableForUser"
DataContext="{StaticResource GeneralVM}"
Command="{Binding BtnToggleLblVisibilityDelegateCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CommandParameter="{Binding LblVisibilityCloseButton}"
Content="{m:Translate ClosingAvailableForUser}"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"
Margin="6,0,0,0"
HorizontalAlignment="Left" VerticalAlignment="Center" />


On another View there's the buttons:



<Button DataContext="{StaticResource GeneralVM}"
Visibility="{Binding LblVisibilityCloseButton, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource NewButtonStyle}"
Grid.Column="3"
Grid.Row="3"
PreviewMouseDown="ImQuit_PreviewMouseDown"
HorizontalAlignment="Right"
VerticalAlignment="Bottom" >
<Image Name="imQuit"
Source="/CWI;component/Images/quit.png"
Stretch="None"/>
</Button>


Here's the code in the ViewModel:



    public Visibility LblVisibilityCloseButton
{
get => _LblVisibilityCloseButton;
set
{
OnPropertyChanged(nameof(LblVisibilityCloseButton));
_LblVisibilityCloseButton = value;
}
}
private Visibility _LblVisibilityCloseButton;

public GeneralViewModel()
{
LblVisibilityCloseButton = Visibility.Visible;
BtnToggleLblVisibilityDelegateCommand = new DelegateCommand<object>(ToggleVisibility);
}

public DelegateCommand<object> BtnToggleLblVisibilityDelegateCommand { get; set; }

private void ToggleVisibility(object obj)
{

if (LblVisibilityCloseButton == Visibility.Visible)
{
LblVisibilityCloseButton = Visibility.Hidden;
}
else
{
LblVisibilityCloseButton = Visibility.Visible;
}
}


Now the thing is that this checkbox works perfect. But I want multiple other checkboxes calling the same command without repeating myself and put 5 other if statements for 5 additionnal buttons.



Edit: I'm using MVVM, I don't want code behind stuff for every single checkbox.










share|improve this question

























  • Possible duplicate of How do I make a textbox visible and hidden with a checkbox?

    – ASh
    Nov 23 '18 at 12:21











  • No it's not. I'm using commands, I don't want any code behind to handle all the stuff. And the problem isn't at all around visiblity or not, it's about making code so I don't repeat myself. All the code above works fine.

    – Selim
    Nov 23 '18 at 12:25













  • I think that the idea here is to have multiple checkboxes, calling a same command to change the different button visibilities in the view model, in order to diminish the number of commands/functions

    – ygosteli
    Nov 23 '18 at 12:42














1












1








1


1






I'm currently doing a program where I want to have multiple checkboxes and multiple buttons where each button is bound to one checkbox. When the checkbox is enabled, it should hide the specific button.



For the moment I can do this for one button:



<CheckBox Name="cbxIsClosableForUser"
DataContext="{StaticResource GeneralVM}"
Command="{Binding BtnToggleLblVisibilityDelegateCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CommandParameter="{Binding LblVisibilityCloseButton}"
Content="{m:Translate ClosingAvailableForUser}"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"
Margin="6,0,0,0"
HorizontalAlignment="Left" VerticalAlignment="Center" />


On another View there's the buttons:



<Button DataContext="{StaticResource GeneralVM}"
Visibility="{Binding LblVisibilityCloseButton, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource NewButtonStyle}"
Grid.Column="3"
Grid.Row="3"
PreviewMouseDown="ImQuit_PreviewMouseDown"
HorizontalAlignment="Right"
VerticalAlignment="Bottom" >
<Image Name="imQuit"
Source="/CWI;component/Images/quit.png"
Stretch="None"/>
</Button>


Here's the code in the ViewModel:



    public Visibility LblVisibilityCloseButton
{
get => _LblVisibilityCloseButton;
set
{
OnPropertyChanged(nameof(LblVisibilityCloseButton));
_LblVisibilityCloseButton = value;
}
}
private Visibility _LblVisibilityCloseButton;

public GeneralViewModel()
{
LblVisibilityCloseButton = Visibility.Visible;
BtnToggleLblVisibilityDelegateCommand = new DelegateCommand<object>(ToggleVisibility);
}

public DelegateCommand<object> BtnToggleLblVisibilityDelegateCommand { get; set; }

private void ToggleVisibility(object obj)
{

if (LblVisibilityCloseButton == Visibility.Visible)
{
LblVisibilityCloseButton = Visibility.Hidden;
}
else
{
LblVisibilityCloseButton = Visibility.Visible;
}
}


Now the thing is that this checkbox works perfect. But I want multiple other checkboxes calling the same command without repeating myself and put 5 other if statements for 5 additionnal buttons.



Edit: I'm using MVVM, I don't want code behind stuff for every single checkbox.










share|improve this question
















I'm currently doing a program where I want to have multiple checkboxes and multiple buttons where each button is bound to one checkbox. When the checkbox is enabled, it should hide the specific button.



For the moment I can do this for one button:



<CheckBox Name="cbxIsClosableForUser"
DataContext="{StaticResource GeneralVM}"
Command="{Binding BtnToggleLblVisibilityDelegateCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CommandParameter="{Binding LblVisibilityCloseButton}"
Content="{m:Translate ClosingAvailableForUser}"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"
Margin="6,0,0,0"
HorizontalAlignment="Left" VerticalAlignment="Center" />


On another View there's the buttons:



<Button DataContext="{StaticResource GeneralVM}"
Visibility="{Binding LblVisibilityCloseButton, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource NewButtonStyle}"
Grid.Column="3"
Grid.Row="3"
PreviewMouseDown="ImQuit_PreviewMouseDown"
HorizontalAlignment="Right"
VerticalAlignment="Bottom" >
<Image Name="imQuit"
Source="/CWI;component/Images/quit.png"
Stretch="None"/>
</Button>


Here's the code in the ViewModel:



    public Visibility LblVisibilityCloseButton
{
get => _LblVisibilityCloseButton;
set
{
OnPropertyChanged(nameof(LblVisibilityCloseButton));
_LblVisibilityCloseButton = value;
}
}
private Visibility _LblVisibilityCloseButton;

public GeneralViewModel()
{
LblVisibilityCloseButton = Visibility.Visible;
BtnToggleLblVisibilityDelegateCommand = new DelegateCommand<object>(ToggleVisibility);
}

public DelegateCommand<object> BtnToggleLblVisibilityDelegateCommand { get; set; }

private void ToggleVisibility(object obj)
{

if (LblVisibilityCloseButton == Visibility.Visible)
{
LblVisibilityCloseButton = Visibility.Hidden;
}
else
{
LblVisibilityCloseButton = Visibility.Visible;
}
}


Now the thing is that this checkbox works perfect. But I want multiple other checkboxes calling the same command without repeating myself and put 5 other if statements for 5 additionnal buttons.



Edit: I'm using MVVM, I don't want code behind stuff for every single checkbox.







c# wpf xaml prism






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:48







Selim

















asked Nov 23 '18 at 12:17









SelimSelim

175




175













  • Possible duplicate of How do I make a textbox visible and hidden with a checkbox?

    – ASh
    Nov 23 '18 at 12:21











  • No it's not. I'm using commands, I don't want any code behind to handle all the stuff. And the problem isn't at all around visiblity or not, it's about making code so I don't repeat myself. All the code above works fine.

    – Selim
    Nov 23 '18 at 12:25













  • I think that the idea here is to have multiple checkboxes, calling a same command to change the different button visibilities in the view model, in order to diminish the number of commands/functions

    – ygosteli
    Nov 23 '18 at 12:42



















  • Possible duplicate of How do I make a textbox visible and hidden with a checkbox?

    – ASh
    Nov 23 '18 at 12:21











  • No it's not. I'm using commands, I don't want any code behind to handle all the stuff. And the problem isn't at all around visiblity or not, it's about making code so I don't repeat myself. All the code above works fine.

    – Selim
    Nov 23 '18 at 12:25













  • I think that the idea here is to have multiple checkboxes, calling a same command to change the different button visibilities in the view model, in order to diminish the number of commands/functions

    – ygosteli
    Nov 23 '18 at 12:42

















Possible duplicate of How do I make a textbox visible and hidden with a checkbox?

– ASh
Nov 23 '18 at 12:21





Possible duplicate of How do I make a textbox visible and hidden with a checkbox?

– ASh
Nov 23 '18 at 12:21













No it's not. I'm using commands, I don't want any code behind to handle all the stuff. And the problem isn't at all around visiblity or not, it's about making code so I don't repeat myself. All the code above works fine.

– Selim
Nov 23 '18 at 12:25







No it's not. I'm using commands, I don't want any code behind to handle all the stuff. And the problem isn't at all around visiblity or not, it's about making code so I don't repeat myself. All the code above works fine.

– Selim
Nov 23 '18 at 12:25















I think that the idea here is to have multiple checkboxes, calling a same command to change the different button visibilities in the view model, in order to diminish the number of commands/functions

– ygosteli
Nov 23 '18 at 12:42





I think that the idea here is to have multiple checkboxes, calling a same command to change the different button visibilities in the view model, in order to diminish the number of commands/functions

– ygosteli
Nov 23 '18 at 12:42












3 Answers
3






active

oldest

votes


















1














Forget the viewmodel way of doing this and use pure xaml solution.



<CheckBox Name="cbxIsClosableForUser"/>
<Button Visibility="{Binding ElementName=cbxIsClosableForUser, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


All you need to do is then to write your BooleanToVisibilityConverter, which implements the IValueConverter interface, load that as app resources with specific name as "BooleanToVisibilityConverter" or what ever you want and you are good to go.



If they are not in the same view, but they do share the viewmodel, then do this



<CheckBox Name="cbxIsClosableForUser" IsChecked="{Binding IsClosableChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Visibility="{Binding IsClosableChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


where IsClosableChecked is your view models boolean property, the viewmodel should implement the INotifyPropertyChanged interface properly, and notify changes in setter of the IsClosableChecked property.






share|improve this answer


























  • I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

    – ygosteli
    Nov 23 '18 at 12:44











  • Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

    – Selim
    Nov 23 '18 at 12:46











  • Appended my answer to contain answer to your scenario.

    – Janne Matikainen
    Nov 23 '18 at 12:53











  • BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

    – ASh
    Nov 23 '18 at 13:02











  • That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

    – Janne Matikainen
    Nov 23 '18 at 13:07





















0














You just need to set PreviewMouseDown= to the same method. I hope that I am not wrong, but e variable of type EventArgs can contain information about the button, that called that event.






share|improve this answer
























  • Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

    – Selim
    Nov 23 '18 at 12:26











  • You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

    – Daniel Martinek
    Nov 23 '18 at 12:41



















0














You can use RelayCommands. You can link all the buttons to the same Command and pass an aditional CommandParamter from your xaml file. The command parameter can be handled in your VM.



Your XAML:



<Button x:Name"Button1" Command="{Binding MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" ..... />


VM:



    RelayCommand _myCommand;
public ICommand MyCommand
{
get
{
if (_myCommand== null)
{
_myCommand= new RelayCommand(param =>
{
if(((Button)param).Name == "Button1"){
//Do what you wish to do with Button1 Click
}
});
}
return _myCommand;
}
}


I have not run this code. But, the solution is on the similar lines. You may pass some other unique parameter to your VM, on which you can distinguish which Button has triggered this command. I am passing Button itself in my parameter. You can pass an integer etc also, using "clr-namespace:System;assembly=mscorlib"






share|improve this answer
























    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',
    autoActivateHeartbeat: false,
    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%2f53446590%2fhow-to-make-less-code-with-multiple-checkboxes-that-call-the-same-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Forget the viewmodel way of doing this and use pure xaml solution.



    <CheckBox Name="cbxIsClosableForUser"/>
    <Button Visibility="{Binding ElementName=cbxIsClosableForUser, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    All you need to do is then to write your BooleanToVisibilityConverter, which implements the IValueConverter interface, load that as app resources with specific name as "BooleanToVisibilityConverter" or what ever you want and you are good to go.



    If they are not in the same view, but they do share the viewmodel, then do this



    <CheckBox Name="cbxIsClosableForUser" IsChecked="{Binding IsClosableChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Visibility="{Binding IsClosableChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    where IsClosableChecked is your view models boolean property, the viewmodel should implement the INotifyPropertyChanged interface properly, and notify changes in setter of the IsClosableChecked property.






    share|improve this answer


























    • I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

      – ygosteli
      Nov 23 '18 at 12:44











    • Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

      – Selim
      Nov 23 '18 at 12:46











    • Appended my answer to contain answer to your scenario.

      – Janne Matikainen
      Nov 23 '18 at 12:53











    • BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

      – ASh
      Nov 23 '18 at 13:02











    • That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

      – Janne Matikainen
      Nov 23 '18 at 13:07


















    1














    Forget the viewmodel way of doing this and use pure xaml solution.



    <CheckBox Name="cbxIsClosableForUser"/>
    <Button Visibility="{Binding ElementName=cbxIsClosableForUser, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    All you need to do is then to write your BooleanToVisibilityConverter, which implements the IValueConverter interface, load that as app resources with specific name as "BooleanToVisibilityConverter" or what ever you want and you are good to go.



    If they are not in the same view, but they do share the viewmodel, then do this



    <CheckBox Name="cbxIsClosableForUser" IsChecked="{Binding IsClosableChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Visibility="{Binding IsClosableChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    where IsClosableChecked is your view models boolean property, the viewmodel should implement the INotifyPropertyChanged interface properly, and notify changes in setter of the IsClosableChecked property.






    share|improve this answer


























    • I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

      – ygosteli
      Nov 23 '18 at 12:44











    • Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

      – Selim
      Nov 23 '18 at 12:46











    • Appended my answer to contain answer to your scenario.

      – Janne Matikainen
      Nov 23 '18 at 12:53











    • BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

      – ASh
      Nov 23 '18 at 13:02











    • That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

      – Janne Matikainen
      Nov 23 '18 at 13:07
















    1












    1








    1







    Forget the viewmodel way of doing this and use pure xaml solution.



    <CheckBox Name="cbxIsClosableForUser"/>
    <Button Visibility="{Binding ElementName=cbxIsClosableForUser, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    All you need to do is then to write your BooleanToVisibilityConverter, which implements the IValueConverter interface, load that as app resources with specific name as "BooleanToVisibilityConverter" or what ever you want and you are good to go.



    If they are not in the same view, but they do share the viewmodel, then do this



    <CheckBox Name="cbxIsClosableForUser" IsChecked="{Binding IsClosableChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Visibility="{Binding IsClosableChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    where IsClosableChecked is your view models boolean property, the viewmodel should implement the INotifyPropertyChanged interface properly, and notify changes in setter of the IsClosableChecked property.






    share|improve this answer















    Forget the viewmodel way of doing this and use pure xaml solution.



    <CheckBox Name="cbxIsClosableForUser"/>
    <Button Visibility="{Binding ElementName=cbxIsClosableForUser, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    All you need to do is then to write your BooleanToVisibilityConverter, which implements the IValueConverter interface, load that as app resources with specific name as "BooleanToVisibilityConverter" or what ever you want and you are good to go.



    If they are not in the same view, but they do share the viewmodel, then do this



    <CheckBox Name="cbxIsClosableForUser" IsChecked="{Binding IsClosableChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Visibility="{Binding IsClosableChecked, Converter={StaticResource BooleanToVisibilityConverter}}"/>


    where IsClosableChecked is your view models boolean property, the viewmodel should implement the INotifyPropertyChanged interface properly, and notify changes in setter of the IsClosableChecked property.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 12:47

























    answered Nov 23 '18 at 12:40









    Janne MatikainenJanne Matikainen

    4,4561017




    4,4561017













    • I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

      – ygosteli
      Nov 23 '18 at 12:44











    • Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

      – Selim
      Nov 23 '18 at 12:46











    • Appended my answer to contain answer to your scenario.

      – Janne Matikainen
      Nov 23 '18 at 12:53











    • BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

      – ASh
      Nov 23 '18 at 13:02











    • That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

      – Janne Matikainen
      Nov 23 '18 at 13:07





















    • I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

      – ygosteli
      Nov 23 '18 at 12:44











    • Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

      – Selim
      Nov 23 '18 at 12:46











    • Appended my answer to contain answer to your scenario.

      – Janne Matikainen
      Nov 23 '18 at 12:53











    • BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

      – ASh
      Nov 23 '18 at 13:02











    • That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

      – Janne Matikainen
      Nov 23 '18 at 13:07



















    I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

    – ygosteli
    Nov 23 '18 at 12:44





    I think that the checkbox and button aren't in the same view, but maybe OP can give more infos on this ?

    – ygosteli
    Nov 23 '18 at 12:44













    Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

    – Selim
    Nov 23 '18 at 12:46





    Well basically there's a view for all the checkboxes and a view for the buttons. These two views are bound to the same ViewModel. I should have been more precise there.

    – Selim
    Nov 23 '18 at 12:46













    Appended my answer to contain answer to your scenario.

    – Janne Matikainen
    Nov 23 '18 at 12:53





    Appended my answer to contain answer to your scenario.

    – Janne Matikainen
    Nov 23 '18 at 12:53













    BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

    – ASh
    Nov 23 '18 at 13:02





    BooleanToVisibilityConverter returns Visible for true, and Collapsed for false. Collapsed is different from Hidden which is used in ToggleVisibility method: Hidden element stil claims the space on the screen as if it was Visible.

    – ASh
    Nov 23 '18 at 13:02













    That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

    – Janne Matikainen
    Nov 23 '18 at 13:07







    That is all up to the implementation of the converter does it return hidden or visible, and how the user interface should work in that the case the checkbox is checked. Better way might to just disable the button if checkbox is not checked.

    – Janne Matikainen
    Nov 23 '18 at 13:07















    0














    You just need to set PreviewMouseDown= to the same method. I hope that I am not wrong, but e variable of type EventArgs can contain information about the button, that called that event.






    share|improve this answer
























    • Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

      – Selim
      Nov 23 '18 at 12:26











    • You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

      – Daniel Martinek
      Nov 23 '18 at 12:41
















    0














    You just need to set PreviewMouseDown= to the same method. I hope that I am not wrong, but e variable of type EventArgs can contain information about the button, that called that event.






    share|improve this answer
























    • Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

      – Selim
      Nov 23 '18 at 12:26











    • You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

      – Daniel Martinek
      Nov 23 '18 at 12:41














    0












    0








    0







    You just need to set PreviewMouseDown= to the same method. I hope that I am not wrong, but e variable of type EventArgs can contain information about the button, that called that event.






    share|improve this answer













    You just need to set PreviewMouseDown= to the same method. I hope that I am not wrong, but e variable of type EventArgs can contain information about the button, that called that event.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 12:25









    Daniel MartinekDaniel Martinek

    1




    1













    • Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

      – Selim
      Nov 23 '18 at 12:26











    • You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

      – Daniel Martinek
      Nov 23 '18 at 12:41



















    • Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

      – Selim
      Nov 23 '18 at 12:26











    • You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

      – Daniel Martinek
      Nov 23 '18 at 12:41

















    Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

    – Selim
    Nov 23 '18 at 12:26





    Re-read, I'm talking about checkboxes, not buttons. And I don't want methods in code behind.

    – Selim
    Nov 23 '18 at 12:26













    You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

    – Daniel Martinek
    Nov 23 '18 at 12:41





    You sed there, that you want to do something after you check/ uncheck any checkbox. (basically hide button) in a method you point to from PrewiewMouseDown. That means I does not have answer, becouse I donˇt know XAML much.

    – Daniel Martinek
    Nov 23 '18 at 12:41











    0














    You can use RelayCommands. You can link all the buttons to the same Command and pass an aditional CommandParamter from your xaml file. The command parameter can be handled in your VM.



    Your XAML:



    <Button x:Name"Button1" Command="{Binding MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" ..... />


    VM:



        RelayCommand _myCommand;
    public ICommand MyCommand
    {
    get
    {
    if (_myCommand== null)
    {
    _myCommand= new RelayCommand(param =>
    {
    if(((Button)param).Name == "Button1"){
    //Do what you wish to do with Button1 Click
    }
    });
    }
    return _myCommand;
    }
    }


    I have not run this code. But, the solution is on the similar lines. You may pass some other unique parameter to your VM, on which you can distinguish which Button has triggered this command. I am passing Button itself in my parameter. You can pass an integer etc also, using "clr-namespace:System;assembly=mscorlib"






    share|improve this answer




























      0














      You can use RelayCommands. You can link all the buttons to the same Command and pass an aditional CommandParamter from your xaml file. The command parameter can be handled in your VM.



      Your XAML:



      <Button x:Name"Button1" Command="{Binding MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" ..... />


      VM:



          RelayCommand _myCommand;
      public ICommand MyCommand
      {
      get
      {
      if (_myCommand== null)
      {
      _myCommand= new RelayCommand(param =>
      {
      if(((Button)param).Name == "Button1"){
      //Do what you wish to do with Button1 Click
      }
      });
      }
      return _myCommand;
      }
      }


      I have not run this code. But, the solution is on the similar lines. You may pass some other unique parameter to your VM, on which you can distinguish which Button has triggered this command. I am passing Button itself in my parameter. You can pass an integer etc also, using "clr-namespace:System;assembly=mscorlib"






      share|improve this answer


























        0












        0








        0







        You can use RelayCommands. You can link all the buttons to the same Command and pass an aditional CommandParamter from your xaml file. The command parameter can be handled in your VM.



        Your XAML:



        <Button x:Name"Button1" Command="{Binding MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" ..... />


        VM:



            RelayCommand _myCommand;
        public ICommand MyCommand
        {
        get
        {
        if (_myCommand== null)
        {
        _myCommand= new RelayCommand(param =>
        {
        if(((Button)param).Name == "Button1"){
        //Do what you wish to do with Button1 Click
        }
        });
        }
        return _myCommand;
        }
        }


        I have not run this code. But, the solution is on the similar lines. You may pass some other unique parameter to your VM, on which you can distinguish which Button has triggered this command. I am passing Button itself in my parameter. You can pass an integer etc also, using "clr-namespace:System;assembly=mscorlib"






        share|improve this answer













        You can use RelayCommands. You can link all the buttons to the same Command and pass an aditional CommandParamter from your xaml file. The command parameter can be handled in your VM.



        Your XAML:



        <Button x:Name"Button1" Command="{Binding MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}" ..... />


        VM:



            RelayCommand _myCommand;
        public ICommand MyCommand
        {
        get
        {
        if (_myCommand== null)
        {
        _myCommand= new RelayCommand(param =>
        {
        if(((Button)param).Name == "Button1"){
        //Do what you wish to do with Button1 Click
        }
        });
        }
        return _myCommand;
        }
        }


        I have not run this code. But, the solution is on the similar lines. You may pass some other unique parameter to your VM, on which you can distinguish which Button has triggered this command. I am passing Button itself in my parameter. You can pass an integer etc also, using "clr-namespace:System;assembly=mscorlib"







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 13:13









        shruti singhshruti singh

        628




        628






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446590%2fhow-to-make-less-code-with-multiple-checkboxes-that-call-the-same-command%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]