WPF新手,百度上找了很久。没有找到对应的答案。以下是写的源代码。设置了Name值。但是在CS中无法使用。我需要做的就是得到所有的ToggleButton,然后将所有的IsChecked属性全部修改为False。在线急求
<UserControl.Resources>
<Style TargetType="{x:Type ToggleButton}" x:Key="selectProject">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
<DataTemplate x:Key="ProjectTemplate" >
<ToggleButton Height="80" Width="145" Content="{Binding Name}" x:Name="toggleButtons" Style="{StaticResource selectProject}" Click="ButtonBase_OnClick">
</ToggleButton>
</DataTemplate>
</UserControl.Resources>
<Grid ShowGridLines="True">
<ItemsControl Width="964" Height="440" BorderBrush="Black" BorderThickness="2" x:Name="pList" ItemTemplate="{StaticResource ProjectTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
toggleButtons.IsChecked
哦,你这是资源文件
你把
<Style TargetType="{x:Type ToggleButton}" x:Key="selectProject">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
这段代码放到App.xaml,如下:
<Application.Resources>
<Style TargetType="{x:Type ToggleButton}" x:Key="selectProject">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
MainWindows.xaml里使用:
<ToggleButton Height="80" Width="145" Content="{Binding Name}" x:Name="toggleButtons" Style="{StaticResource selectProject}" Checked="toggleButtons_Checked">
</ToggleButton>
cs里:
private void toggleButtons_Checked(object sender, RoutedEventArgs e)
{
toggleButtons.IsChecked = false;
}
@iEvent: 谢谢了 呵呵