這個的晚上想學學WPF 做一個類似于ERP 左邊菜單。構思一下思路 。。。。。。
建立一個類基于Expander類。
1 public class ExpanderBox : Expander
2 {
3 #region 定義依賴屬性
4
5
6 //定義ItemsSource資料源
7 public static readonly DependencyProperty ItemsSourcesProperty = DependencyProperty.Register("ItemsSources", typeof(object), typeof(ExpanderBox));
8 //定義ItemsCommand ,Header頭部點選指令。
9 public static readonly DependencyProperty ItemsCommandProperty = DependencyProperty.Register("ItemsCommand", typeof(ICommand), typeof(ExpanderBox));
10 //定義HeaderHeight,Header頭部的高度。
11 public static readonly DependencyProperty HeaderHeightProperty = DependencyProperty.Register("HeaderHeight", typeof(int), typeof(ExpanderBox));
12
13 public object ItemsSources
14 {
15 get { return (object)GetValue(ExpanderBox.ItemsSourcesProperty); }
16 set { SetValue(ExpanderBox.ItemsSourcesProperty, value); }
17 }
18 public ICommand ItemsCommand
19 {
20 get { return (ICommand)GetValue(ExpanderBox.ItemsCommandProperty); }
21 set { SetValue(ExpanderBox.ItemsSourcesProperty, value); }
22 }
23
24 public int HeaderHeight
25 {
26 get { return (int)GetValue(ExpanderBox.HeaderHeightProperty); }
27 set { SetValue(ExpanderBox.HeaderHeightProperty, value); }
28 }
29 #endregion
30
31 //public ExpanderBox()
32 //{
33 // HeaderHeight = 10;
34 //}
35
36
37 }
由于 Expander中Content中要放一個ListBox,是以定義了一個依賴屬性ItemsSources,HeaderHeight是頭部的高度。
後然構造好了,就要寫前台的樣式了。
1 <Style TargetType="{x:Type local:ExpanderBox}">
2 <Setter Property="Template">
3 <Setter.Value>
4 <ControlTemplate TargetType ="local:ExpanderBox">
5 <Grid>
6 <Grid.RowDefinitions>
7 <RowDefinition ></RowDefinition>
8 <RowDefinition></RowDefinition>
9 </Grid.RowDefinitions>
10 <ToggleButton Grid.Row="0" Height="{TemplateBinding HeaderHeight}" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Content="{TemplateBinding Header}" Command="{TemplateBinding ItemsCommand}"></ToggleButton>
11 <ListBox x:Name="itemsBox" Grid.Row="1" ItemsSource="{TemplateBinding ItemsSources}" Visibility="Collapsed" ></ListBox>
12 </Grid>
13 <ControlTemplate.Triggers>
14 <Trigger Property="Expander.IsExpanded" Value="true">
15 <Setter Property="Visibility" Value="Visible" TargetName="itemsBox"></Setter>
16 </Trigger>
17 </ControlTemplate.Triggers>
18 </ControlTemplate>
19 </Setter.Value>
20 </Setter>
21 </Style>
從代碼框中看到一條有背景色代碼,為什麼要标出來呢!請不要急,繼續看下文
在視窗中定義ExpanderBox
1 <local:ExpanderBox x:Name="ex" Header="采購合同" HeaderHeight="40" ItemsSources="{Binding Lists}"></local:ExpanderBox>
2 <local:ExpanderBox x:Name="ex1" IsExpanded="True" Header="采購合同" HeaderHeight="{Binding Height}" ItemsSources="{Binding Lists}"></local:ExpanderBox>
如我所料,運作結果如圖!

但是我發現,我無論怎樣設定HeaderHeight的值,都無法改變ToggleButton高度。
在網上找了一些資料,很幸運的是在微軟網站中找到答案,我就不多寫了,直接貼上原文
網址:http://msdn.microsoft.com/zh-cn/library/ms742882.aspx
對于模闆方案來說,TemplateBinding 是綁定的優化形式,類似于使用 {Binding RelativeSource={RelativeSource TemplatedParent}} 構造的 Binding。 TemplateBinding 始終為單向綁定,即使所涉及的屬性預設為雙向綁定。 所涉及的兩個屬性都必須是依賴項屬性。
RelativeSource 是另一個标記擴充,有時與 TemplateBinding 結合使用或者代替它使用,以便在模闆中執行相對屬性綁定。
此處未介紹控件模闆概念;有關詳細資訊,請參閱 Control 樣式和模闆。
特性文法是最常用于該标記擴充的文法。 在 TemplateBinding 辨別符字元串之後提供的字元串标記被指定為基礎 TemplateBindingExtension 擴充類的 Property 值。
對象元素文法也可行,但因為沒有實際的應用,是以未進行示範。 TemplateBinding 用于使用計算的表達式來填充資源庫内的值,是以使用 TemplateBinding 的對象元素文法來填充 <Setter.Property> 屬性元素文法就會變得繁冗而多餘。
TemplateBinding 還可以在詳細特性用法中使用,以便将 Property 屬性指定為一個 property=value 對:
看完這句話不能明白上面有兩種背景顔色的代碼。前面是單向綁定,是以在前台指派無效,而後者是雙向綁定。
總結:
TemplateBinding 是單向綁定形式。
Binding是多向的。
以上隻是個人想法和實踐經驗,如果有文字錯誤和文法錯誤,請加以指點!
QQ:247039968
emil:[email protected]
無論是美女的歌聲,還是鬣狗的狂吠,無論是鳄魚的眼淚,還是惡狼的嚎叫,都不會使我動搖
以上隻是個人想法和實踐經驗所得,如果有文字錯誤和文法錯誤,請加以指點!