检测是最后一个子控件并更改一个属性

时间:2013-10-09 14:45:41

标签: c# wpf

我有一个包含许多列表框的堆栈面板,除了最后一个列表框之外的所有列表框都应该有背景可见。是否可以从ControlTemplate中检测到列表框是最后一个子项并将“Bd”不透明度设置为0.(MyListBox扩展了Listbox)

<Style TargetType="{x:Type local:MyListBox}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:MyListBox}">
             <Grid >
             ...
                <Border Grid.Row="0" x:Name="Bd"...>
                   <Border.Background>
                      <ImageBrush Stretch="Fill" ImageSource="/arrayProba1;component/bck/mybck.png" />
                   </Border.Background> 

如果我添加另一个列表框,以前最后一个孩子的旧版本将获得背景,而新版本将丢失它,设置“Bd”不透明度0(稍后将设置动画)

private List<MyListBox> MyPanels = new List<MyListBox>();

MyPanels.Add(new MyListBox() { Title = "..." });
MyPanelsHolder.Children.Add(MyPanels[n]);

我让它在后面的代码中创建整个样式,但我需要这样做。 你会如何处理这个问题?

1 个答案:

答案 0 :(得分:0)

感谢Tony,我最终是怎么做到的...... 那个边界现在是

<Border Visibility="{TemplateBinding IsNotCurrent, Converter={StaticResource BoolToVis}}">
  <Border.Background>
    <ImageBrush Stretch="Fill" ImageSource="/arrayProba1;component/Bck/MyBck.png" />

在MyListBox类中我有:

public static readonly DependencyProperty IsNotCurrentProperty =
  DependencyProperty.Register("IsNotCurrent", typeof(bool),
  typeof(MyListBox), new FrameworkPropertyMetadata(false));

public bool IsNotCurrent
{
  get { return (bool)GetValue(IsNotCurrentProperty); }
  set { SetValue(IsNotCurrentProperty, value); }
}

因此,每次添加或删除listBox时,我都会设置所有MyPanels [i] .IsNotCurrent = true;并将最后一个设置为'false'。

我可能会在以后更改为肯定版本(而不是IsNotCurrent到IsLast)