这是前台
<!-DataGrid -->
<DataGrid DockPanel.Dock="Top" FontSize="15" Background="White" VerticalGridLinesBrush="#FFC9C9F9" HorizontalGridLinesBrush="#FFB7D7F7" Margin="8,0,8,0" ScrollViewer.VerticalScrollBarVisibility="Visible" Name="grid" Grid.Row="1" SelectionUnit="FullRow" SelectionMode="Single" IsReadOnly="True" AlternationCount="2" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn FontSize="15" Header="序号" Width="100" Binding="{Binding Number}"/>
<DataGridTextColumn FontSize="15" Header="描述" Width="200" Binding="{Binding Describe}" />
<DataGridTextColumn FontSize="15" Header="路径" Width="*" Binding="{Binding Path}" />
<DataGridTemplateColumn Header="操作" Width="300">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="Show" GroupName="{Binding Number}" Width="50" VerticalAlignment="Center" Margin="20 0 15 0" Checked="Show_Checked">显示</RadioButton>
<RadioButton x:Name="Hide" GroupName="{Binding Number}" Width="50" VerticalAlignment="Center" Checked="Hide_Checked">隐藏</RadioButton>
<RadioButton x:Name="Start" GroupName="{Binding IsStart}" Width="50" VerticalAlignment="Center" Margin="15 0 15 0" Checked="Start_Checked">启动</RadioButton>
<RadioButton x:Name="Close" GroupName="{Binding IsStart}" Width="50" VerticalAlignment="Center" Checked="Close_Checked">关闭</RadioButton>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="StartAll" Grid.Column="2" HorizontalAlignment="Center" Click="StartAll_Click" FontSize="15">全部</Button>
我想点击全部的时候,模板里所有的启动按钮都选中,请问后台代码应该怎么写???
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var stackPanel= this.GetTemplateChild("xxxx").AsType<StackPanel>();
yourRadio.Checked += (o,e)=>{stackPanel.Children.OfType<RadioButton>().ForEach(t=>{t.IsChecked=true;})};
}
~~大致如此
wpf已经很灵活了,不是非要搞到模版中——尤其是 不是变更原始类的样子。
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var stackPanel= this.GetTemplateChild("xxxx").AsType<StackPanel>();
yourRadio.Checked += (o,e)=>{stackPanel.Children.OfType<RadioButton>().ForEach(t=>{t.IsChecked=true;})};
}