博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

动态更改按钮的可视状态及VisualTreeHelper的使用

Posted on 2010-08-05 17:56  小高好孩子  阅读(470)  评论(0)    收藏  举报

 

 

在做项目的过程中,遇到一个需求:动态更listbox中某一按钮的可视状态,用到的知识点有两个

1。VisualStateManager.GoToState(Control control, string stateName, bool useTransitions);  

2.VisualTreeHelper.GetChild(DependencyObject reference, int childIndex)

 我的listbox代码如下:

 

代码
1 <ListBox x:Name="lboxTeamNature" VerticalAlignment="Top" Grid.Column="0" Height="480">
2 <ListBox.Template>
3 <ControlTemplate>
4 <ScrollViewer Height="480" BorderThickness="0" VerticalScrollBarVisibility="Auto">
5 <ItemsPresenter />
6 </ScrollViewer>
7 </ControlTemplate>
8 </ListBox.Template>
9 <ListBox.ItemsPanel>
10 <ItemsPanelTemplate>
11 <controlsToolkit:WrapPanel HorizontalAlignment="Left" ItemWidth="110" VerticalAlignment="Top">
12 </controlsToolkit:WrapPanel>
13 </ItemsPanelTemplate>
14 </ListBox.ItemsPanel>
15 <ListBox.ItemTemplate>
16 <DataTemplate>
17 <Button Name="btnSelected" Click="btnSelected_Click" Margin="5,10" Loaded="btnSelected_Loaded" Style="{StaticResource ButtonStyle4}" Width="90" Height="70" HorizontalAlignment="Center" VerticalAlignment="Center">
18 <Button.Content>
19 <StackPanel>
20 <Image Width="48" Height="48" Source="{Binding IsUse,Converter={StaticResource ConvertIsUse},Mode=OneTime}"></Image>
21 <TextBlock Text="{Binding Name}" TextAlignment="Center" HorizontalAlignment="Center" />
22 <!--<TextBlock Text="{Binding IsUse,Converter={StaticResource ConvertIsUse}}" Foreground="{Binding IsUse,Converter={StaticResource ConvertToColor}}"/>-->
23
24 <!--<StackPanel Orientation="Vertical">
25 <StackPanel Orientation="Horizontal">
26 <TextBlock Text="名称:" />
27 <TextBlock Text="{Binding Name}" />
28 </StackPanel>
29 <StackPanel Orientation="Horizontal">
30 <TextBlock Text="代码:" />
31 <TextBlock Text="{Binding Code}" />
32 </StackPanel>
33 <StackPanel Orientation="Horizontal">
34 <TextBlock Text="状态:" />
35 <TextBlock Text="{Binding IsUse,Converter={StaticResource ConvertBoolToString}}" Foreground="{Binding IsUse,Converter={StaticResource ConvertToColor}}"/>
36 </StackPanel>
37 </StackPanel>-->
38 </StackPanel>
39 </Button.Content>
40 </Button>
41 </DataTemplate>
42 </ListBox.ItemTemplate>
43 </ListBox>

 

 

为了设置其中一项的按钮的可视状态,写出了下面这些垃圾代码。

我感觉这样是不值的,可没办法,这是经理要求的结果,哪位有更好的解决方法请留言,共同学习学习。^_^

代码
1 ScrollViewer scrollViewer = (VisualTreeHelper.GetChild(this.lboxTeamNature, 0) as ScrollViewer);
2 Border border = VisualTreeHelper.GetChild(scrollViewer, 0) as Border;
3 Grid grid = VisualTreeHelper.GetChild(border, 0) as Grid;
4 ScrollContentPresenter scrollContentPresenter = VisualTreeHelper.GetChild(grid, 0) as ScrollContentPresenter;
5 ItemsPresenter itemsPresenter = VisualTreeHelper.GetChild(scrollContentPresenter, 0) as ItemsPresenter;
6 System.Windows.Controls.WrapPanel wrapPanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as System.Windows.Controls.WrapPanel;
7 ListBoxItem listBoxItem = VisualTreeHelper.GetChild(wrapPanel, index) as ListBoxItem;
8 Grid listBoxItemgrid = VisualTreeHelper.GetChild(listBoxItem, 0) as Grid;
9 ContentPresenter contentPresenter = VisualTreeHelper.GetChild(listBoxItemgrid, 2) as ContentPresenter;
10 Button btn = VisualTreeHelper.GetChild(contentPresenter, 0) as Button;
11 VisualStateManager.GoToState(btn, stateName, true);