如何访问Itemtemplate Silverlight中的控件
我有一个列表框。我有一个带有堆栈面板的项目模板。 它在项目模板中有一个文本框和一个复选框 是否有办法访问该复选框并在所选索引更改时启用/禁用它如何访问Itemtemplate Silverlight中的控件,silverlight,listbox,Silverlight,Listbox,我有一个列表框。我有一个带有堆栈面板的项目模板。 它在项目模板中有一个文本框和一个复选框 是否有办法访问该复选框并在所选索引更改时启用/禁用它 这里有一个使用单选按钮的解决方案: 将其更改为复选框应该很容易 假设您的项目资源已绑定: <ListBox x:Name="myList"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientati
这里有一个使用单选按钮的解决方案:将其更改为复选框应该很容易 假设您的项目资源已绑定:
<ListBox x:Name="myList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Check, Mode=TwoWay}" />
<TextBlock Text="{Binding Name, Mode=TwoWay}"
Width="100" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="button1"
Content="Uncheck 2"
Click="button1_Click" />
当然,这里的问题是,如果禁用复选框,您将无法访问Click环境
另一种选择是,当列表框上的选择更改时,使用VisualTreeHelper获取对复选框的引用:
private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox list = sender as ListBox;
ListInfo current = list.SelectedItem as ListInfo;
List<CheckBox> checkBoxes = new List<CheckBox>();
getCheckBoxes(list, ref checkBoxes);
foreach (CheckBox checkBox in checkBoxes)
{
if (checkBox.Content.ToString() == current.Name)
{
checkBox.Foreground = new SolidColorBrush(Colors.Red);
}
}
}
public void getCheckBoxes(UIElement parent, ref List<CheckBox> items)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = VisualTreeHelper.GetChild(parent, i) as UIElement;
if (child.GetType() == typeof(CheckBox))
{
items.Add(child as CheckBox);
}
getCheckBoxes(child, ref items);
}
}
}
private void myList\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
ListBox list=发送方作为ListBox;
ListInfo current=list.SelectedItem作为ListInfo;
列表复选框=新建列表();
获取复选框(列表、参考复选框);
foreach(复选框中的复选框)
{
if(checkBox.Content.ToString()==current.Name)
{
checkBox.Foreground=新的SolidColorBrush(Colors.Red);
}
}
}
public void getcheckbox(UIElement父项、ref列表项)
{
int count=VisualTreeHelper.GetChildrenCount(父级);
如果(计数>0)
{
for(int i=0;i
这当然不是提高性能的最佳选择,但您可以获得更大的灵活性。我正在尝试访问UI属性以启用/禁用复选框。设置复选框的值没有问题。例如,如果我想更改listboxitem容器的背景色或其他属性,我希望能够在代码隐藏中执行此操作。对此表示抱歉!我更新了答案,我希望这确实有帮助,如果没有,请告诉我;但它并没有给我子项,是不是因为我正在为listbox项itemtemplate使用静态资源?它没有给我足够的空间把XAML放在这里。我将尝试使用XAML更新原始帖子。根据我在您的XAML中看到的内容,您正在尝试复选框。单击“方法”,对吗?我建议将VisualTreeHelper与列表框上的SelectionChanged事件一起使用。如果您使用CheckBox.Click事件,您可以使用以下代码获得复选框引用:CheckBox chk=sender as CheckBox;实际上.click事件执行另一个操作。我没有,把孩子带到那里。但我正试图根据页面中其他部分的某些逻辑启用/禁用某些复选框。我正试图在代码隐藏中访问listboxitem的容器。我有一个带有项目模板的列表框。项目模板有一个堆栈面板,其中有一个文本框和一个复选框。我试图访问代码隐藏中某些逻辑上的复选框并禁用它。我希望能够访问控件,chkbox.IsEnabled=false
<ListBox x:Name="myList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Check, Mode=TwoWay}"
IsEnabled="True"
Content="{Binding Name, Mode=TwoWay}"
Click="CheckBox_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox chk = sender as CheckBox;
chk.IsEnabled = false;
}
private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox list = sender as ListBox;
ListInfo current = list.SelectedItem as ListInfo;
List<CheckBox> checkBoxes = new List<CheckBox>();
getCheckBoxes(list, ref checkBoxes);
foreach (CheckBox checkBox in checkBoxes)
{
if (checkBox.Content.ToString() == current.Name)
{
checkBox.Foreground = new SolidColorBrush(Colors.Red);
}
}
}
public void getCheckBoxes(UIElement parent, ref List<CheckBox> items)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = VisualTreeHelper.GetChild(parent, i) as UIElement;
if (child.GetType() == typeof(CheckBox))
{
items.Add(child as CheckBox);
}
getCheckBoxes(child, ref items);
}
}
}