2013-02-19 64 views
1

所以,我有一個代碼:如何在DataTemplate中控制?

<tool:LongListSelector x:Name="citiesListGropus" Background="Transparent" 
    ItemTemplate="{StaticResource citiesItemTemplate}"      
    GroupHeaderTemplate="{StaticResource groupHeaderTemplate}" 
    GroupItemTemplate="{StaticResource groupItemTemplate}" Height="468" Margin="0,68,0,0"> 
     <tool:LongListSelector.GroupItemsPanel> 
     <ItemsPanelTemplate> 
      <tool:WrapPanel/> 
     </ItemsPanelTemplate> 
     </tool:LongListSelector.GroupItemsPanel> 
</tool:LongListSelector> 

而且有DataTemplate

<DataTemplate x:Key="groupHeaderTemplate" x:Name="groupHeaderTemplateName"> 
    <Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" Margin="-333, 15, 0, 15" x:Name="groupHeaderName"> 
     <TextBlock Text="{Binding Title}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/> 
    </Border> 
</DataTemplate> 

我怎樣才能更改屬性的DataTemplateBorderVisibility?我想隱藏DataTemplate。但是我不能將數據綁定到屬性,如TextBlock Text="{Binding Title}",因爲我只做了一次綁定數據,然後我需要在數據綁定後更改一些屬性。

任何想法?

更新1

所以,我的主要目標是過濾城市名單。我想按名稱過濾它們。

我的算法:

1)我從WebService獲取數據。

2)加載我ViewModel

if (!App.CitiesViewModel.IsDataLoaded) 
    { 
     App.CitiesViewModel.LoadData(serviceResponse.Result); 
    } 

3)將通過名稱的城市。我的代碼像this code。 這很重要,特別是對於LongListSeletstor我必須使用模板。

好的,我的數據準備好了。現在我不需要Web服務。

<!-- The template for city items --> 
     <DataTemplate x:Key="citiesItemTemplate"> 
      <StackPanel Name="PlacePanel" 
       Orientation="Horizontal" Margin="0,0,0,17" 
       Tag="{Binding Id}" Visibility="{Binding IsVisibility}"> 
       <Image Height="75" Width="75" HorizontalAlignment="Left" Margin="12,0,9,0" Name="Image" Stretch="Fill" VerticalAlignment="Top" Source="{Binding Image}"/> 
       <StackPanel Width="311"> 
        <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
       </StackPanel> 
      </StackPanel> 
     </DataTemplate> 

我有TextBox,當我一個城市的類型名稱 - 不必要的城市隱藏。我可以使用數據綁定和INotifyPropertyChanged,所以我可以隱藏城市。

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      TextBox name = (TextBox)sender; 

      foreach(var place in App.CitiesViewModel.Items 
       .Where(city => (city.Name.Contains(name.Text)) && (city.IsShow == true))) 
      { 
       city.IsVisibility = Visibility.Visible; 
      } 
      foreach (var city in App.CitiesViewModel.Items 
       .Where(city => !city.Name.Contains(name.Text))) 
      { 
       city.IsVisibility = Visibility.Collapsed; 
      } 
     } 

但我有一些問題,當我搜索城市,我無法掩蓋這個DataTemplate

<DataTemplate x:Key="groupHeaderTemplate" x:Name="groupHeaderTemplateName"> 
    <Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" Margin="-333, 15, 0, 15" x:Name="groupHeaderName"> 
     <TextBlock Text="{Binding Title}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/> 
    </Border> 
</DataTemplate> 

當我鍵入一個城市我想隱藏GroupHeaderTemplate的名稱。當TextBox失去焦點時顯示GroupHeaderTemplateenter image description here

我借過這張圖片here

更新2 我的錯誤是我沒有使用PropertyChangedEventHandler。將此添加到GroupCity屬性並實現接口INotifyPropertyChanged後,我可以在數據綁定後更改屬性Data Template

private IList<Group<City>> citiesCollectionGroup; 

    var cityByName = from city in App.ViewModel.Items 
group city by Convert.ToString(City.Name[0]).ToLower() 
into c orderby c.Key select new 
GroupCity<CitiesViewModel>(Convert.ToString(c.Key[0]).ToLower(), c);  
citiesCollectionGroup = cityByName.ToList(); 
this.citiesListGropus.ItemsSource = citiesCollectionGroup; 

public class GroupCity<T> : IEnumerable<T>, INotifyPropertyChanged 
    { 
     public GroupCity(string name, IEnumerable<T> items) 
     { 
      this.Title = name; 
      this.IsVisibility = Visibility.Visible; 
      this.Items = new List<T>(items);    
     } 

     public override bool Equals(object obj) 
     { 
      GroupCity<T> that = obj as GroupCity<T>; 

      return (that != null) && (this.Title.Equals(that.Title)); 
     } 

     public string Title 
     { 
      get; 
      set; 
     } 

     private Visibility _isVisibility; 
     public Visibility IsVisibility 
     { 
      get 
      { 
       return _isVisibility; 
      } 
      set 
      { 
       _isVisibility = value; 
       NotifyPropertyChanged("IsVisibility"); 
      } 
     } 

     public IList<T> Items 
     { 
      get; 
      set; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void NotifyPropertyChanged(String propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (null != handler) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     #region IEnumerable<T> Members 

     public IEnumerator<T> GetEnumerator() 
     { 
      return this.Items.GetEnumerator(); 
     } 

     #endregion 

     #region IEnumerable Members 

     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
     { 
      return this.Items.GetEnumerator(); 
     } 

     #endregion 
    } 

XAML代碼:

<DataTemplate x:Key="groupHeaderTemplate" x:Name="groupHeaderTemplateName"> 
    <Border Background="{StaticResource PhoneAccentBrush}" Width="75" Height="75" Margin="-333, 15, 0, 15" x:Name="groupHeaderName" Visibility="{Binding IsVisibility}"> 
     <TextBlock Text="{Binding Title}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/> 
    </Border> 
</DataTemplate> 

現在,我們可以Visibility groupHeaderTemplate。

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      foreach (GroupCity<CitiesViewModel> city in citiesCollectionGroup) 
      { 
       city.IsVisibility = Visibility.Collapsed; 
      } 
      // 
     } 



private void SearchText_LostFocus(object sender, RoutedEventArgs e) 
     { 
      foreach (GroupCity<CitiesViewModel> city in citiesCollectionGroup) 
      { 
       city.IsVisibility = Visibility.Visible; 
      } 
      // 
     } 

回答

1

隨着「我怎麼能在DataTemplate中改變邊框的屬性Visibility?我想隱藏的DataTemplate。」我知道你想改變基於屬性的項目的知名度,請參見下面的例子。

(如果下面的代碼不完全是您正在尋找的解決方案,請提供一些關於您想要實現的更多信息,重新閱讀您的問題以及我對這個示例的評論,我懷疑這是你想做什麼,但我不想猜,所以我只是回答我對你問題的解釋)。

隨着綁定可以:

  1. 綁定可見性屬性直接向(與列表框和PERSON1例如)
  2. 綁定可見性屬性的布爾屬性類型能見度的AA屬性,和使用轉換器將其轉換爲可見性類型(listbox2和Person2 + BoolToVis類)。

有更多的方法,但這是兩個最簡單的在我opinion.¨實施的下面的代碼

結果: screen shot of VS

查看:

<phone:PhoneApplicationPage 
x:Class="VisibilityWP.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
xmlns:VisibilityWP="clr-namespace:VisibilityWP" 
shell:SystemTray.IsVisible="True"> 

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid.Resources> 
       <VisibilityWP:BoolToVis x:Key="BooleanToVisibilityConverter" /> 
     </Grid.Resources> 
     <StackPanel> 
      <ListBox x:Name="listBox" Height="300"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Border Width="200" Visibility="{Binding Visibility}" Background="Blue"> 
          <TextBlock Text="{Binding Name}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/> 
         </Border> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
      <ListBox x:Name="listBox2" Height="300"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Border Width="200" Visibility="{Binding ShowBorder, Converter={StaticResource BooleanToVisibilityConverter}}" Background="Red"> 
          <TextBlock Text="{Binding Name}" FontSize="40" Foreground="White" Margin="15, 15, 0, 0"/> 
         </Border> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </StackPanel> 

    </Grid> 
</Grid> 

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Data; 
namespace VisibilityWP 
{ 

    public partial class MainPage 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      listBox.ItemsSource = new List<Person1> 
        { 
         new Person1 {Name = "Iris", Visibility = Visibility.Visible}, 
         new Person1 {Name = "Somebody", Visibility = Visibility.Collapsed}, 
         new Person1 {Name = "Peter", Visibility = Visibility.Visible}, 

        }; 
      listBox2.ItemsSource = new List<Person2> 
        { 
         new Person2 {Name = "Iris", ShowBorder = true}, 
         new Person2 {Name = "Mia", ShowBorder = true}, 
         new Person2 {Name = "Somebody", ShowBorder = false} 
        }; 
     } 
    } 

    public class Person1 
    { 
     public string Name { get; set; } 
     public Visibility Visibility { get; set; } 
    } 
    public class Person2 
    { 
     public string Name { get; set; } 
     public bool ShowBorder { get; set; } 
    } 

    public sealed class BoolToVis : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return value is Visibility && (Visibility)value == Visibility.Visible; 
     } 
    } 
} 
+0

請注意,亞歷山大可能會在他的列表中看到這種方式的差距。他可能想過濾掉一些項目。 – 2013-02-19 14:36:12

+1

@Iris這看起來好像對我的格式很好(看到你的推文)。 – 2013-02-19 14:53:30

+0

我實際上對這個問題有點困惑,他是否要求如何在運行時檢索資源,過濾/分組或可見性?我解釋了他想要使用綁定來更改可見性屬性的問題。這可以做到,但是據說這可能不是最好的做法,如果意圖是過濾數據。 – 2013-02-19 15:09:22

2

您可以編寫自己的簡單DataTemplateSelector來執行此操作。查詢this瞭解更多信息。

+0

謝謝!我現在查看這篇文章。在示例中,我可以更改TextBlock的屬性Foreground。但是,我不明白,我們如何改變這一點? – Alexandr 2013-02-20 06:23:40

相關問題