2011-08-16 73 views
1

我有這樣一段代碼在我的MainPage.xaml中:WP7 - 如何在資源 - 訪問按鈕屬性>風格

<phone:PhoneApplicationPage.Resources> 
     <Style x:Key="ListBoxStyle" TargetType="ListBox"> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
      <Setter Property="Padding" Value="0"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBox"> 
         <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}"> 
          <StackPanel> 
           <ItemsPresenter/> 
           <Button x:Name="LoadMoreButton" Content="Load more data..." Background="{StaticResource PhoneAccentBrush}" Click="LoadMoreData_Click" Visibility="Collapsed" /> 
          </StackPanel> 
         </ScrollViewer> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </phone:PhoneApplicationPage.Resources> 

我的問題是如何訪問例如LoadMoreButton.Visibility財產MainPage.xaml中的.cs?

如果我嘗試使用LoadMoreButton.Visibility,intelisense沒有返回任何東西。我想你不能使用這樣的資源元素,但我希望你們中的一些人知道解決這個「問題」。先謝謝你!

+1

我知道這是不是你要找的東西,但這裏有一個列表一個很好的例子和代碼在底部時自動加載更多。它工作的很好,你可以放置一個TextBlock和一個不確定的進度條,說'加載更多...',用戶在底部時會看到它。 http://blog.slimcode.com/2010/09/11/detect-when-a-listbox-scrolls-to-its-end-wp7/ –

+0

謝謝..有趣的方式來實現加載更多的行爲..我會檢查它:) – rjovic

回答

0

簡版:你不行。

長版:你不能,我不明白你爲什麼會。關於在ListBoxItem內部有一個名爲的的整個想法,意思是多次,沒有任何意義。

真的想做的,就是將可見度綁定到一個屬性。考慮使用ValueConverter(搜索BoolToVisibilityConverter,你會發現),所以你可以簡單地綁定正在綁定數據的項目的布爾屬性。

+0

問題中的按鈕不是在itme中,而是整個ListBox的模板。儘管 –

+0

Hmmf,我會推薦綁定方法,我想我在他的代碼中讀取ListBoxItem作爲TargetType。愚蠢的眼睛。 –

0

使用該輔助函數:

 public static ChildItem FindVisualChild<ChildItem>(DependencyObject obj) 
      where ChildItem : DependencyObject { 
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { 
       DependencyObject child = VisualTreeHelper.GetChild(obj, i); 
       if (child != null && child is ChildItem) { 
        return (ChildItem)child; 
       } else { 
        ChildItem childOfChild = FindVisualChild<ChildItem>(child); 
        if (childOfChild != null) { 
         return childOfChild; 
        } 
       } 
      } 
      return null; 
     } 

要找到按鈕:

var button = FindVisualChild<Button>(listbox);