2008-10-21 49 views
1

我有一個由ToolBarTray和ToolBar組成的ControlTemplate。在我的工具欄中,我有幾個按鈕,然後是一個標籤。我希望能夠更新我的工具欄中的標籤,例如「1/10」如何更新WPF中工具欄的ControlTemplate中的標籤?

我的第一個想法是以編程方式查找標籤並對其進行設置,但我在閱讀時應該使用觸發器完成此操作。我很難理解如何做到這一點。有任何想法嗎?

<Style x:Key="DocViewerToolBarStyle" TargetType="{x:Type ContentControl}"> 
    <Setter Property="Template"> 
    <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ContentControl}"> 
       <ToolBarTray... /> 
       <ToolBar.../> 
       <Button../>    
       <Button..> 

      <Label x:Name="myStatusLabel" .. /> 

回答

1

ControlTemplate的用途是定義控件的外觀。對於您的問題,我不確定控制模板是否是正確的解決方案。

正如Bryan指出的那樣,您應該將標籤的內容屬性綁定到您的控件中已存在的屬性。這應該通過TemplateBinding完成。

<Label x:Name="myStatusLabel" Content={TemplateBinding MyStatusLabelProperty} ../> 

酒店MyStatusLabelProperty則具有控件類存在。 通常,您將創建自己的UserControl,它具有名爲MyStatusLabelProperty的正確類型(對象或字符串)的依賴項屬性。

+0

在哪裏以及如何創建MyStatusLabelProperty? – ScottG 2008-10-22 18:15:58

1

我會將標籤設置爲控件的「內容」屬性,例如,

<Label x:Name="myStatusLabel" Content="{TemplateBinding Content}"/> 

然後,您可以使用頂級對象的內容屬性設置標籤的文本。

+0

我的內容是一個DocumentViewer。它沒有內容屬性。我需要一個自定義屬性,但不知道在哪裏/如何設置它。 – ScottG 2008-10-22 18:40:01

0

我會創造一個實現INotifyPropertyChanged接口視圖模型,並使用DataTemplate中使用這樣的事情來顯示它:

<DataTemplate DataType={x:Type viewmodel:MyToolBarViewModel}> 
    <Label Content={Binding CurrentPage} /> 
    <Label Content={Binding TotalPages} ContentStringFormat="{}of {0}" /> 
</DataTemplate> 

<ToolBar> 
    <ContentPresenter Content={Binding <PathtoViewModel>} /> 
</ToolBar> 

隨着使用綁定你沒有明確地更新標籤的內容。您所要做的就是在視圖模型中設置屬性的值,並引發適當的PropertyChanged事件,這會導致標籤更新其內容。

相關問題