How do I retrieve the controls created by a DataTemplate?












-1















I have a list of objects that I need to represent as a list of buttons.
These buttons should normally act as a regular Button; when the checkbox is checked, they should work as ToggleButtons and remain pressed. But I also need them to be mutually exclusive, like a RadioButton (only one can only be toggled at any time).



I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control).



So I thought to use a ToggleButton as the template, manually uncheck it if the checkbox is not checked, and manually handle the mutual exclusion.
However, I can't find a way to retrieve the toggle buttons for the other items in the list to uncheck them.



Here's my XAML:



<Window x:Class="WpfApp9.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">

<UniformGrid Rows="1">
<UniformGrid.Resources>
<DataTemplate x:Key="template">
<ToggleButton Name="Toggle"
Checked="ToggleButton_Checked"
Content="{Binding}"/>
</DataTemplate>
</UniformGrid.Resources>

<ItemsControl Name="lst" ItemTemplate="{StaticResource template}" />

<CheckBox Name="CheckToggle"
HorizontalAlignment="Center"
VerticalAlignment="Center">
TOGGLE
</CheckBox>
</UniformGrid>
</Window>


And this is my code-behind:



using System.Windows;
using System.Windows.Controls.Primitives;

namespace WpfApp9
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
lst.ItemsSource = new { "foo", "bar", "baz" };
}

private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
var toggle = (ToggleButton)sender;

// If the checkbox is not checked, release the button immediately
if (CheckToggle.IsChecked != true)
toggle.IsChecked = false;

// now how do I uncheck the other ToggleButtons?
}
}
}









share|improve this question



























    -1















    I have a list of objects that I need to represent as a list of buttons.
    These buttons should normally act as a regular Button; when the checkbox is checked, they should work as ToggleButtons and remain pressed. But I also need them to be mutually exclusive, like a RadioButton (only one can only be toggled at any time).



    I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control).



    So I thought to use a ToggleButton as the template, manually uncheck it if the checkbox is not checked, and manually handle the mutual exclusion.
    However, I can't find a way to retrieve the toggle buttons for the other items in the list to uncheck them.



    Here's my XAML:



    <Window x:Class="WpfApp9.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"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

    <UniformGrid Rows="1">
    <UniformGrid.Resources>
    <DataTemplate x:Key="template">
    <ToggleButton Name="Toggle"
    Checked="ToggleButton_Checked"
    Content="{Binding}"/>
    </DataTemplate>
    </UniformGrid.Resources>

    <ItemsControl Name="lst" ItemTemplate="{StaticResource template}" />

    <CheckBox Name="CheckToggle"
    HorizontalAlignment="Center"
    VerticalAlignment="Center">
    TOGGLE
    </CheckBox>
    </UniformGrid>
    </Window>


    And this is my code-behind:



    using System.Windows;
    using System.Windows.Controls.Primitives;

    namespace WpfApp9
    {
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    lst.ItemsSource = new { "foo", "bar", "baz" };
    }

    private void ToggleButton_Checked(object sender, RoutedEventArgs e)
    {
    var toggle = (ToggleButton)sender;

    // If the checkbox is not checked, release the button immediately
    if (CheckToggle.IsChecked != true)
    toggle.IsChecked = false;

    // now how do I uncheck the other ToggleButtons?
    }
    }
    }









    share|improve this question

























      -1












      -1








      -1








      I have a list of objects that I need to represent as a list of buttons.
      These buttons should normally act as a regular Button; when the checkbox is checked, they should work as ToggleButtons and remain pressed. But I also need them to be mutually exclusive, like a RadioButton (only one can only be toggled at any time).



      I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control).



      So I thought to use a ToggleButton as the template, manually uncheck it if the checkbox is not checked, and manually handle the mutual exclusion.
      However, I can't find a way to retrieve the toggle buttons for the other items in the list to uncheck them.



      Here's my XAML:



      <Window x:Class="WpfApp9.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"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">

      <UniformGrid Rows="1">
      <UniformGrid.Resources>
      <DataTemplate x:Key="template">
      <ToggleButton Name="Toggle"
      Checked="ToggleButton_Checked"
      Content="{Binding}"/>
      </DataTemplate>
      </UniformGrid.Resources>

      <ItemsControl Name="lst" ItemTemplate="{StaticResource template}" />

      <CheckBox Name="CheckToggle"
      HorizontalAlignment="Center"
      VerticalAlignment="Center">
      TOGGLE
      </CheckBox>
      </UniformGrid>
      </Window>


      And this is my code-behind:



      using System.Windows;
      using System.Windows.Controls.Primitives;

      namespace WpfApp9
      {
      public partial class MainWindow : Window
      {
      public MainWindow()
      {
      InitializeComponent();
      lst.ItemsSource = new { "foo", "bar", "baz" };
      }

      private void ToggleButton_Checked(object sender, RoutedEventArgs e)
      {
      var toggle = (ToggleButton)sender;

      // If the checkbox is not checked, release the button immediately
      if (CheckToggle.IsChecked != true)
      toggle.IsChecked = false;

      // now how do I uncheck the other ToggleButtons?
      }
      }
      }









      share|improve this question














      I have a list of objects that I need to represent as a list of buttons.
      These buttons should normally act as a regular Button; when the checkbox is checked, they should work as ToggleButtons and remain pressed. But I also need them to be mutually exclusive, like a RadioButton (only one can only be toggled at any time).



      I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control).



      So I thought to use a ToggleButton as the template, manually uncheck it if the checkbox is not checked, and manually handle the mutual exclusion.
      However, I can't find a way to retrieve the toggle buttons for the other items in the list to uncheck them.



      Here's my XAML:



      <Window x:Class="WpfApp9.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"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">

      <UniformGrid Rows="1">
      <UniformGrid.Resources>
      <DataTemplate x:Key="template">
      <ToggleButton Name="Toggle"
      Checked="ToggleButton_Checked"
      Content="{Binding}"/>
      </DataTemplate>
      </UniformGrid.Resources>

      <ItemsControl Name="lst" ItemTemplate="{StaticResource template}" />

      <CheckBox Name="CheckToggle"
      HorizontalAlignment="Center"
      VerticalAlignment="Center">
      TOGGLE
      </CheckBox>
      </UniformGrid>
      </Window>


      And this is my code-behind:



      using System.Windows;
      using System.Windows.Controls.Primitives;

      namespace WpfApp9
      {
      public partial class MainWindow : Window
      {
      public MainWindow()
      {
      InitializeComponent();
      lst.ItemsSource = new { "foo", "bar", "baz" };
      }

      private void ToggleButton_Checked(object sender, RoutedEventArgs e)
      {
      var toggle = (ToggleButton)sender;

      // If the checkbox is not checked, release the button immediately
      if (CheckToggle.IsChecked != true)
      toggle.IsChecked = false;

      // now how do I uncheck the other ToggleButtons?
      }
      }
      }






      c# wpf xaml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 17:00









      Michele IppolitoMichele Ippolito

      41839




      41839
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I ended up solving the problem above in a different way.
          In the question I said that




          I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control)




          but I didn't realize I could use the GroupName property to force them into the same group. At this point the template can be this:



              <DataTemplate x:Key="template">
          <RadioButton Checked="RadioButton_Checked"
          GroupName="SomeGroupName"
          Content="{Binding}"/>
          </DataTemplate>


          And I get my mutually exclusive buttons without handling them manually.






          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%2f53305291%2fhow-do-i-retrieve-the-controls-created-by-a-datatemplate%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I ended up solving the problem above in a different way.
            In the question I said that




            I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control)




            but I didn't realize I could use the GroupName property to force them into the same group. At this point the template can be this:



                <DataTemplate x:Key="template">
            <RadioButton Checked="RadioButton_Checked"
            GroupName="SomeGroupName"
            Content="{Binding}"/>
            </DataTemplate>


            And I get my mutually exclusive buttons without handling them manually.






            share|improve this answer




























              0














              I ended up solving the problem above in a different way.
              In the question I said that




              I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control)




              but I didn't realize I could use the GroupName property to force them into the same group. At this point the template can be this:



                  <DataTemplate x:Key="template">
              <RadioButton Checked="RadioButton_Checked"
              GroupName="SomeGroupName"
              Content="{Binding}"/>
              </DataTemplate>


              And I get my mutually exclusive buttons without handling them manually.






              share|improve this answer


























                0












                0








                0







                I ended up solving the problem above in a different way.
                In the question I said that




                I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control)




                but I didn't realize I could use the GroupName property to force them into the same group. At this point the template can be this:



                    <DataTemplate x:Key="template">
                <RadioButton Checked="RadioButton_Checked"
                GroupName="SomeGroupName"
                Content="{Binding}"/>
                </DataTemplate>


                And I get my mutually exclusive buttons without handling them manually.






                share|improve this answer













                I ended up solving the problem above in a different way.
                In the question I said that




                I tried using a RadioButton as the template for my ItemsControl, but they are not mutually exclusive (I guess that they are not actually children of the same control)




                but I didn't realize I could use the GroupName property to force them into the same group. At this point the template can be this:



                    <DataTemplate x:Key="template">
                <RadioButton Checked="RadioButton_Checked"
                GroupName="SomeGroupName"
                Content="{Binding}"/>
                </DataTemplate>


                And I get my mutually exclusive buttons without handling them manually.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 9:44









                Michele IppolitoMichele Ippolito

                41839




                41839






























                    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%2f53305291%2fhow-do-i-retrieve-the-controls-created-by-a-datatemplate%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

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?