2014-01-14 35 views
5

我使用DataTemplate中的的SelectedItem上的ListView爲:ListView中選擇對象使用選擇的DataTemplate

<ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem"> 
<WrapPanel (...) BackGround="Silver"> 

</WrapPanel> 
(...) 
<Style TargetType="ListViewItem"> 
     <Style.Triggers> 
      <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsSelected" Value="true" /> 
         <Condition Property="Selector.IsSelectionActive" Value="true" /> 
        </MultiTrigger.Conditions> 
        <Setter Property="Template" Value="{StaticResource SelectedTemplate}" /> 
      </MultiTrigger> 
     </Style.Triggers> 
</Style> 
</ControlTemplate> 

而且,是的ItemsSource綁定到IObservableCollection。但我在選擇myListView中的項目時遇到了一些問題。我想要達到的是,默認項目模板具有白色背景。選定的背景是銀色。當我點擊物品時,背景會發生變化。但是當我從代碼中做到這一點時,listview選擇了項目(選中index = 0,selectetitem!= null),但項目從未選擇的項目中獲取樣式。所以基本上我想選擇具有selectedTemplate的項目。 我試過myListView.SelectedIndex,myLisview.SelectedItem,但並沒有真正的工作..任何想法?

謝謝!

+0

仍然需要幫助。 – user13657

+0

另外 - 我知道我可以複製DataTemplate並將其複製到選定的控件模板中,但我不想使用此解決方案,因爲每次我在ListView中選擇一個項目時,它都會從我的課程中獲取方法。我需要的只是使用相同的模板更換背景銀。 – user13657

+0

您可以創建一個可重新生成的解決方案嗎?對我們來說呢? – BenjaminPaul

回答

5

如果我正確理解你,你有它的工作,所以當你選擇列表中的項目時,選定的模板銀背景顯示,但是當你使用代碼設置選定的項目時,它不會?

我已經創建了一個可行的簡單的例子,我不得不做出改變是設置列表視圖selectedItem屬性的結合是雙向..

<ListView SelectedItem="{Binding MySelectedItem, Mode=TwoWay}"> 

我的示例使用微卡利然而這並不重要。

演示它的工作我的繼承人的示例代碼...

視圖模型:

public class ShellViewModel : Screen, IShell 
{ 
    private ObservableCollection<Person> people = new ObservableCollection<Person>(); 

    public ObservableCollection<Person> People 
    { 
     get 
     { 
      return this.people; 
     } 

     set 
     { 
      this.people = value; 
      this.NotifyOfPropertyChange(() => this.People); 
     } 
    } 

    private Person selectedPerson; 

    public Person SelectedPerson 
    { 
     get 
     { 
      return this.selectedPerson; 
     } 

     set 
     { 
      this.selectedPerson = value; 
      this.NotifyOfPropertyChange(() => this.SelectedPerson); 
     } 
    } 

    public ShellViewModel() 
    { 
     var russell = new Person { Name = "Russell" }; 
     this.People.Add(new Person { Name = "Benjamin" }); 
     this.People.Add(new Person { Name = "Steve" }); 
     this.People.Add(russell); 
     this.SelectedPerson = russell; 
    } 

查看:

<Window x:Class="WpfApplication5.ShellView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<Window.Resources> 

    <Style x:Key="TextStyle" TargetType="{x:Type TextBlock}"> 
     <Style.Triggers> 
      <DataTrigger 
        Binding="{Binding 
         RelativeSource={RelativeSource 
          Mode=FindAncestor, 
          AncestorType={x:Type ListBoxItem}}, 
          Path=IsSelected}" 
        Value="True"> 
       <Setter Property="Background" Value="Red"></Setter> 
       </DataTrigger> 
     </Style.Triggers> 
    </Style> 

    <DataTemplate x:Key="MyItemTemplate"> 
     <TextBlock Text="{Binding Name}" Style="{StaticResource TextStyle}"></TextBlock> 
    </DataTemplate> 
</Window.Resources> 

<Grid Background="White"> 
    <ListView ItemsSource="{Binding People}" x:Name="People" ItemTemplate="{StaticResource MyItemTemplate}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}"> 
    </ListView> 
</Grid> 

改變在S視圖模型上的electionPerson屬性也顯示在視圖中。

希望這會有所幫助!

0

你有沒有試過在你的風格中設置這個?

<Style> 
    <Setter Property="OverridesDefaultStyle" Value="true"/> 
</Style 

,然後設置模板而不觸發如此完整的代碼是這樣的

<Style TargetType="{x:Type listViewItem}"> 
<Setter Property="OverridesDefaultStyle" Value="True"/> 
<Setter Property="Template" Value="{StaticResource SelectedTemplate}" /> 
</Style> 

我之所以列入這種風格是因爲如果你使用你的控件模板內,改變目標類型來{x:Type ListViewItem}而比targetType="ListViewItem"它會自動分配這種風格的認爲控制。 也許是因爲我的回答是不完整的人投票我的回答我甚至沒有尊嚴來解釋,哦: 這裏是擴展回答: 除了上面的代碼,你應該在你的Person類應該添加屬性IsSelected類應該執行INotifyPropertyChanged並在您的XAML: 「>

現在

在您的視圖模型 你應該有這樣的事情在構造

//lsvProducts is our ListView 
view = (CollectionView) CollectionViewSource.GetDefaultView(lsvPeople.ItemsSource); 
//in the creation parameter you should put field that you want to group by :-) 
PropertyGroupDescription grouping = new PropertyGroupDescription("<FieldToGroupBy>"); 
view.GroupDescriptions.Add(grouping); 

回去XAML

<Style x:Key="GroupedView" TargetType="{x:Type GroupItem}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding ElementName=chbx, Path=IsChecked}" Value="True"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate> 
          <Expander IsExpanded="False"> 
           <Expander.Header> 
            <StackPanel Orientation="Horizontal"> 
             <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" /> 
             <TextBlock Text="{Binding ItemCount}" FontSize="16" Foreground="DimGray" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" /> 
             <TextBlock Text=" item(s)" FontSize="16" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" /> 
            </StackPanel> 
           </Expander.Header> 
           <ItemsPresenter /> 
          </Expander> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </DataTrigger> 

<!--This goes in the same style as the prevoius sample code --> 
    <DataTrigger Binding="{Binding ElementName=chbx, Path=IsChecked}" Value="False"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <StackPanel> 
            <ItemsPresenter /> 
           </StackPanel> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 

注意 當綁定到你可以定義一個在VM說bool IsGrouping並將其綁定到一個元素。 祝你好運:-)

+0

對投票的人 ELABORATE! – XAMlMAX