2016-03-04 57 views
1

最終目標是能夠在列表框中的第一個和最後一個元素上設置特定的ItemContainerStyle;嘗試爲對象集合綁定創建IsFirstItem和IsLastItem IValueConverter

該轉換器迄今有:

public class IsFirstItemConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool result = false; 
     result = ((IList<object>)parameter).First() == value; 

     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
public class IsLastItemConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     bool result = false; 
     result = ((IList<object>)parameter).Last() == value; 

     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

而實現:

<DataTrigger Value="True" Binding="{Binding Converter={StaticResource IsFirstItemConverter},ConverterParameter=Items, ElementName=SubItems}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="First"/> 
        <ContentPresenter/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 

和錯誤是:

InvalidCastException的:無法轉換類型的對象「System.String '到 類型'System.Collections.Generic.IList`1 [System.Object]'。

我確定我搞砸了多個地方,只是沒有足夠的經驗與XAML和綁定縮小在哪裏。

+1

[Click](http://stackoverflow.com/a/12131197/1997232)。 – Sinatr

+0

看看[this](http://stackoverflow.com/questions/13613053/how-can-i-know-if-a-listboxitem-is-the-last-item-inside-a-wpfs-listbox ) –

+0

@Sinatr哦,我的工作比我試圖做的更乾淨,請張貼答案(儘管它只是其他地方的參考),所以我可以接受它。這絕對會被添加到我的常用轉換器庫中。 – Wobbles

回答

1

以下改編自問題的意見很好地工作:

public class IsLastItemConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     DependencyObject item = (DependencyObject)value; 
     ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item); 

     return ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class IsFirstItemConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     DependencyObject item = (DependencyObject)value; 
     ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item); 

     return ic.ItemContainerGenerator.IndexFromContainer(item) == 0; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

public class IsOnlyItemConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     DependencyObject item = (DependencyObject)value; 
     ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item); 

     return (ic.ItemContainerGenerator.IndexFromContainer(item) == 0 && ic.ItemContainerGenerator.Items.Count == 1); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

實現:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource IsLastItemConverter}}" Value="True"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="└"/> 
        <ContentPresenter/> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</DataTrigger> 

魔術似乎是ItemsControlFromItemContainer使得它,所以我不必同時通過該項目,它的收集實例到轉換器,但只是項目和轉換器可以推斷父集合。

相關問題