无法在我的C#代码中访问此reportCheckBox。
我可以在我的代码中访问ReportsListBox,但不能访问reportsCheckBox。
<ListBox x:Name="reportsListBox" Grid.Row="0"
Background="Transparent" BorderBrush="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ItemTemplate="{Binding DynamicReportsList}" Visibility="Visible" Foreground="WhiteSmoke" Grid.RowSpan="2" Margin="156,0,2,0" Grid.ColumnSpan="2"
SelectionMode="Multiple">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<CheckBox x:Name="reportsCheckBox" Margin="5,2" Style="{StaticResource CheckboxStyle}"
IsChecked="{Binding ElementName=DailyReports, Path=IsCheckedBoxChecked}"
Foreground="WhiteSmoke"
FontSize="14">
<ContentPresenter />
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
所以基本上,我希望能够像这样访问我的复选框:
private void btnCancel_Event(object sender, EventArgs e)
{
foreach (CheckBox c in reportsCheckBox)
{
c.isChecked = false;
}
}
答案 0 :(得分:0)
尝试一下:
[已更新]
不是直接添加字符串值,而是像这样使用 ListBoxItem :
dialog
XAML:
reportsListBox.Items.Add("Item1"); // thrown exception
reportsListBox.Items.Add("Item2"); // thrown exception
reportsListBox.Items.Add("Item3"); // thrown exception
reportsListBox.Items.Add(new ListBoxItem {Content = "Item1" });
reportsListBox.Items.Add(new ListBoxItem {Content = "Item2" });
reportsListBox.Items.Add(new ListBoxItem {Content = "Item3" });
C#:
<ListBox x:Name="reportsListBox"
Grid.Row="0"
Background="Transparent" BorderBrush="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ItemTemplate="{Binding DynamicReportsList}" Visibility="Visible" Foreground="WhiteSmoke" Grid.RowSpan="2" Margin="156,0,2,0" Grid.ColumnSpan="2"
SelectionMode="Multiple">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
Content="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<!-- This is my sample items -->
<ListBoxItem Content="Item 1"/>
<ListBoxItem Content="Item 2"/>
<ListBoxItem Content="Item 3"/>
<ListBoxItem Content="Item 4"/>
</ListBox>
输出:
答案 1 :(得分:0)
因此,本文的第一个答案实际上解决了我的问题。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/81e92a50-d0a2-4e70-93ab-e7b374858087/how-to-get-the-check-boxes-which-are-dynamically-created-in-xaml?forum=wpf
也可以代替cb.IsEnabled = false;我做了cb.IsChecked = false;此外,我从 AmRo的注释中设置了绑定。因此,有了适当的绑定,并借助了本文的帮助,这解决了我的问题。
XAML代码:
<ListBox x:Name="reportsListBox" Grid.Row="0"
Background="Transparent" BorderBrush="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ItemTemplate="{Binding DynamicReportsList}" Visibility="Visible" Foreground="WhiteSmoke" Grid.RowSpan="2" Margin="4,0,0,0"
SelectionMode="Multiple" Grid.Column="1">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<CheckBox Name="reportsCheckBox" IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
Content="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
Style="{StaticResource CheckboxStyle}" Foreground="WhiteSmoke" FontSize="20" IsEnabled="{Binding enabled}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
C#代码:
private void btnCancel_Event(object sender, EventArgs e)
{
foreach (var item in reportsListBox.Items)
{
ListBoxItem lbi = reportsListBox.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
CheckBox cb = FindVisualChild<CheckBox>(lbi);
if (cb != null)
cb.IsChecked = false;
}
}