How to uncheck CheckBox on button click in WPF using C#?

ぐ巨炮叔叔 提交于 2019-12-24 19:01:01

问题


I have a CheckBox in GridControl Column. After performing some operation the selected checkboxes inside GridControl must be UNCHECKED on button click in WPF. Any idea?

<dxg:GridControl Name="grdInfill"  Height="700" VerticalAlignment="Center">
    <dxg:GridControl.Columns>
        <dxg:GridColumn  AllowEditing="True">
            <dxg:GridColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="chkSelect"  HorizontalAlignment="Center" 
                        IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=GlassType}"  
                        Checked="CheckEdit_Checked" 
                        Unchecked="CheckEdit_Unchecked" />
                 </DataTemplate>
             </dxg:GridColumn.CellTemplate>
         </dxg:GridColumn>
     </dxg:GridControl.Columns>
     <dxg:GridControl.View>
         <dxg:TableView Name="grdInfillInner"  ShowTotalSummary="True" AutoWidth="True" 
             DetailHeaderContent="True"  ShowIndicator="False" ShowGroupPanel="False" 
             CellValueChanging="grdInfillInner_CellValueChanging">
             <!--GroupRowTemplate="{StaticResource descriptionHeader}"-->
         </dxg:TableView>
     </dxg:GridControl.View>
</dxg:GridControl>
<Button Name="BtnClearAllCheckbox" Content="Clear All Checkbox" Height="20" Width="80" />

Help Appreciated!


回答1:


Try below.......

 <CheckBox Name="chkSelect"  HorizontalAlignment="Center" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=GlassType,Mode=TwoWay}"

Im guessing there should be property "GlassType"

Public Bool GlassType {get;set;}

use TwoWay mode while binding and set property value GlassType as True false....




回答2:


give an Uid to ur checkbox

<CheckBox Uid="CheckAll" />

than use this extension method to find the element inside the dataTemplate--->

public static UIElement FindUid(this DependencyObject parent, string uid)
{
    var count = VisualTreeHelper.GetChildrenCount(parent);
    if (count == 0) return null;

    for (int i = 0; i < count; i++)
    {
        var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
        if (el == null) continue;

        if (el.Uid == uid) return el;

        el = el.FindUid(uid);
        if (el != null) return el;
    }
    return null;
}

Access the CheckBox in code behind like this

CheckBox checkBox = myDataGrid.FindUid("chkSelect") as CheckBox;


来源:https://stackoverflow.com/questions/16784265/how-to-uncheck-checkbox-on-button-click-in-wpf-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!