基于继承类的属性模版中无法绑定的原因和解决方法
這個的晚上想學學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:wujc@younger.com
無論是美女的歌聲,還是鬣狗的狂吠,無論是鱷魚的眼淚,還是惡狼的嚎叫,都不會使我動搖
總結
以上是生活随笔為你收集整理的基于继承类的属性模版中无法绑定的原因和解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。