C# 如何使启用时触发的复选框点击动作为false
在我的wp8应用程序中,我在列表框中有复选框,我希望能够在启用为false时点击复选框。我的意思是我想点击复选框,但选中时不应更改C# 如何使启用时触发的复选框点击动作为false,c#,windows-phone-8,checkbox,C#,Windows Phone 8,Checkbox,在我的wp8应用程序中,我在列表框中有复选框,我希望能够在启用为false时点击复选框。我的意思是我想点击复选框,但选中时不应更改 <ListBox Name="URLListBox" Height="Auto" Grid.Row="2" > <ListBox.ItemTemplate> <DataTemplate> <Grid Background="Transparent" Margin="0,0,0,10
<ListBox Name="URLListBox" Height="Auto" Grid.Row="2" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent" Margin="0,0,0,10" >
<CheckBox IsChecked="{Binding file}" x:Name="surename" Tag="{Binding b1Tag}" Grid.Column="0" FontSize="25" Content="{Binding text}" Tap="surename_Tap" VerticalAlignment="Center" HorizontalAlignment="Center"></CheckBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您可以通过在Listbox内声明该复选框并勾选Listbox点击事件上的复选框来进行编辑。像这样,
<ListBox Name="listBox" Tap="listBox_Tap">
<CheckBox Content="sample" IsEnabled="False" Name="checkBox" />
</ListBox>
如果该列表框中有多个复选框,请在列表框点击事件中获取SelectedIndex并更改该复选框值。在您的例子中,您可以从以下代码中引用xaml
<ListBox Grid.Row="1" Tap="RoleslistBox_Tap" Name="RoleslistBox">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsEnabled="False" Content="{Binding RoleTypes}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在RoleslistBox点击事件中,你可以写
private void RoleslistBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
int i = RoleslistBox.SelectedIndex;
ListBoxItem item = this.RoleslistBox.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
CheckBox tagregCheckBox = FindFirstElementInVisualTree<CheckBox>(item);
tagregCheckBox.IsChecked = true;
}
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
private void RoleslistBox\u点击(对象发送方,System.Windows.Input.GestureEventArgs e)
{
int i=角色列表框。选择的索引;
ListBoxItem=this.RoleslistBox.ItemContainerGenerator.ContainerFromIndex(i)作为ListBoxItem;
复选框tagregCheckBox=FindFirstElementVisualTree(项目);
tagregCheckBox.IsChecked=true;
}
私有T FindFirstElementVisualTree(DependencyObject parentElement),其中T:DependencyObject
{
var count=VisualTreeHelper.GetChildrenCount(parentElement);
如果(计数=0)
返回null;
for(int i=0;i
您不想勾选该复选框,当该复选框被右键点击时?
private void RoleslistBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
int i = RoleslistBox.SelectedIndex;
ListBoxItem item = this.RoleslistBox.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
CheckBox tagregCheckBox = FindFirstElementInVisualTree<CheckBox>(item);
tagregCheckBox.IsChecked = true;
}
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}