0
我在此站點上找到了解決方案(如何在項目控件中的項目之間輸入分隔符(click)並發現bug =(((( )它?(WPF)如何在ItemsControl中的項目之間添加分隔符 - Bug修復
,當我試圖調整ItemsControl的(我已經設置屬性「HorizontalScrollBarVisibility」爲「Disable」)任何想法,我應該如何修復這個bug
我在此站點上找到了解決方案(如何在項目控件中的項目之間輸入分隔符(click)並發現bug =(((( )它?(WPF)如何在ItemsControl中的項目之間添加分隔符 - Bug修復
,當我試圖調整ItemsControl的(我已經設置屬性「HorizontalScrollBarVisibility」爲「Disable」)任何想法,我應該如何修復這個bug
這是不容易這hapens ,但我意識到我應該怎麼做才能解決這個問題。我的想法是使用自定義轉換器,但我沒有任何解決方法r的想法如何將ConverterParameter發送到轉換器。但我找到了解決辦法。 這裏是我的轉換器:
public class SeparatorConverter : IValueConverter
{
private Visibility _visible;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var element = (TextBlock) value;
element.Loaded += ElementLoaded;
return _visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null; //not needed
}
private static void ElementLoaded(object sender, RoutedEventArgs e)
{
var element = sender as TextBlock;
var parentItemsControl = element.FindParent(x => x is ItemsControl) as ItemsControl;
var parentPanel = element.FindParent(x => x is Panel) as Panel;
if (parentItemsControl == null || element == null || parentPanel== null)
return;
var itemText = parentPanel.FindName("MyTextBlock") as TextBlock;
var collection = parentItemsControl.ItemsSource as IEnumerable<string>;
if (itemText == null || collection == null)
return;
var list = collection.ToList();
if (list.IndexOf(itemText.Text) == list.Count() - 1) // Can be incorrect because we can have two same items
element.Visibility = Visibility.Collapsed;
}
}
查找父功能:
public static DependencyObject FindParent(this DependencyObject element, Func<DependencyObject, bool> filter)
{
DependencyObject parent = VisualTreeHelper.GetParent(element);
if (parent != null)
{
if (filter(parent))
{
return parent;
}
return FindParent(parent, filter);
}
return null;
}
這裏是我的XAML代碼:
<Coverters:SeparatorConverter x:Key="SeparatorVisibilityConverter">
</Coverters:SeparatorConverter>
<ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=False}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel>
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock x:Name="MyTextBlock" Text="{Binding}" Style="{StaticResource TextBlockInListViewItem}" TextWrapping="WrapWithOverflow"/>
<TextBlock x:Name="commaTextBlock" Style="{StaticResource TextBlockInListViewItem}" Text=", " Visibility="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource SeparatorVisibilityConverter}}"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
確定。任何方式,如果你有另一個想法如何解決這個錯誤,你可以在這裏發表你的答案=)