开发者

How to un-check radio button that appear in ItemsControl ?

开发者 https://www.devze.com 2023-04-11 23:03 出处:网络
I define ItemControl that contain Radio Button. <ItemsControl x:Name=\"items\" ItemsSource=\"{Binding Path=myItemList}\" >

I define ItemControl that contain Radio Button.

       <ItemsControl x:Name="items" ItemsSource="{Binding Path=myItemList}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton Content="{Binding Path=Key}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Now, I want to un-checked all the radioButton that the ItemsControl contain. I want to write some method that will un-checked all those radioButton. How can i do it ?

Thanks f开发者_如何学运维or any help...


I solve the problem and this is the code ...

foreach( var item in myItemList.Items )
        {
            DependencyObject presenter = CategoryBar.ItemContainerGenerator.ContainerFromItem( item );

            RadioButton t = VisualTreeHelper.GetChild( presenter, 0 ) as RadioButton;

            if( t != null && t.IsChecked == true )
            {
                t.IsChecked = false;
            }
        }


I can see that the radio buttons are binded to myItemList. Add another property (for the example named IsChecked.) Make the method run on myItemList and set all the IsChecked properties to false.

Make sure the IsChecked property raises PropetyChangedEvent.

For example:

If myItemList is List(of Bla) and Bla has the property Key and IsChecked. A simple For each loop can do the job:

For Each blaItem as Bla in myItemList
   blaItem.IsChecked = false
Next

The xaml look like this:

<ItemsControl x:Name="items" ItemsSource="{Binding Path=myItemList}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <RadioButton Content="{Binding Path=Key}" Checked="{Binding Path=IsChecked}/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号