本文整理汇总了C#中ListBox.ApplyTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# ListBox.ApplyTemplate方法的具体用法?C# ListBox.ApplyTemplate怎么用?C# ListBox.ApplyTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ListBox
的用法示例。
在下文中一共展示了ListBox.ApplyTemplate方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataContexts_Should_Be_Correctly_Set
public void DataContexts_Should_Be_Correctly_Set()
{
var items = new object[]
{
"Foo",
new Item("Bar"),
new TextBlock { Text = "Baz" },
new ListBoxItem { Content = "Qux" },
};
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
DataContext = "Base",
DataTemplates = new DataTemplates
{
new FuncDataTemplate<Item>(x => new Button { Content = x })
},
Items = items,
};
target.ApplyTemplate();
var dataContexts = target.Presenter.Panel.Children
.Cast<Control>()
.Select(x => x.DataContext)
.ToList();
Assert.Equal(
new object[] { items[0], items[1], "Base", "Base" },
dataContexts);
}
示例2: LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items
public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
{
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
target.ApplyTemplate();
Assert.Equal(3, target.GetLogicalChildren().Count());
foreach (var child in target.GetLogicalChildren())
{
Assert.IsType<ListBoxItem>(child);
}
}
示例3: Clicking_Item_Should_Select_It
public void Clicking_Item_Should_Select_It()
{
var target = new ListBox
{
Template = new ControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
target.ApplyTemplate();
target.Presenter.Panel.Children[0].RaiseEvent(new PointerPressEventArgs
{
RoutedEvent = InputElement.PointerPressedEvent,
MouseButton = MouseButton.Left,
});
Assert.Equal(0, target.SelectedIndex);
}
示例4: Focusing_Item_With_Arrow_Key_Should_Select_It
public void Focusing_Item_With_Arrow_Key_Should_Select_It()
{
var target = new ListBox
{
Template = new ControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
target.ApplyTemplate();
target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
{
RoutedEvent = InputElement.GotFocusEvent,
NavigationMethod = NavigationMethod.Directional,
});
Assert.Equal(0, target.SelectedIndex);
}
示例5: ListBoxItem_Containers_Should_Be_Generated
public void ListBoxItem_Containers_Should_Be_Generated()
{
var items = new[] { "Foo", "Bar", "Baz " };
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = items,
};
target.ApplyTemplate();
var text = target.Presenter.Panel.Children
.OfType<ListBoxItem>()
.Do(x => x.Template = ListBoxItemTemplate())
.Do(x => x.ApplyTemplate())
.Select(x => x.Presenter.Child)
.OfType<TextBlock>()
.Select(x => x.Text)
.ToList();
Assert.Equal(items, text);
}
示例6: Setting_SelectedItem_Should_Set_Panel_Keyboard_Navigation
public void Setting_SelectedItem_Should_Set_Panel_Keyboard_Navigation()
{
var target = new ListBox
{
Template = new FuncControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
target.ApplyTemplate();
target.Presenter.Panel.Children[1].RaiseEvent(new PointerPressEventArgs
{
RoutedEvent = InputElement.PointerPressedEvent,
MouseButton = MouseButton.Left,
});
var panel = target.Presenter.Panel;
Assert.Equal(
KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel),
panel.Children[1]);
}
示例7: Setting_Item_IsSelected_Sets_ListBox_Selection
public void Setting_Item_IsSelected_Sets_ListBox_Selection()
{
var target = new ListBox
{
Template = new ControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
};
target.ApplyTemplate();
((ListBoxItem)target.GetLogicalChildren().ElementAt(1)).IsSelected = true;
Assert.Equal("Bar", target.SelectedItem);
Assert.Equal(1, target.SelectedIndex);
}
示例8: Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected
public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected()
{
var target = new ListBox
{
Template = new ControlTemplate(CreateListBoxTemplate),
Items = new[] { "Foo", "Bar", "Baz " },
SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
};
target.ApplyTemplate();
target.SelectedIndex = 0;
target.Presenter.Panel.Children[0].RaiseEvent(new PointerPressEventArgs
{
RoutedEvent = InputElement.PointerPressedEvent,
MouseButton = MouseButton.Left,
});
Assert.Equal(0, target.SelectedIndex);
}
示例9: Prepare
private void Prepare(ListBox target)
{
// The ListBox needs to be part of a rooted visual tree.
var root = new TestRoot();
root.Child = target;
// Apply the template to the ListBox itself.
target.ApplyTemplate();
// Then to its inner ScrollViewer.
var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
scrollViewer.ApplyTemplate();
// Then make the ScrollViewer create its child.
((ContentPresenter)scrollViewer.Presenter).UpdateChild();
// Now the ItemsPresenter should be reigstered, so apply its template.
target.Presenter.ApplyTemplate();
// Because ListBox items are virtualized we need to do a layout to make them appear.
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
// Now set and apply the item templates.
foreach (ListBoxItem item in target.Presenter.Panel.Children)
{
item.Template = ListBoxItemTemplate();
item.ApplyTemplate();
item.Presenter.ApplyTemplate();
((ContentPresenter)item.Presenter).UpdateChild();
}
// The items were created before the template was applied, so now we need to go back
// and re-arrange everything.
foreach (IControl i in target.GetSelfAndVisualDescendents())
{
i.InvalidateMeasure();
}
target.Arrange(new Rect(0, 0, 100, 100));
}
示例10: Removing_SelectedItem_Should_Clear_TabOnceActiveElement
public void Removing_SelectedItem_Should_Clear_TabOnceActiveElement()
{
var items = new ObservableCollection<string>(new[] { "Foo", "Bar", "Baz " });
var target = new ListBox
{
Template = Template(),
Items = items,
};
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.Presenter.Panel.Children[1].RaiseEvent(new PointerPressedEventArgs
{
RoutedEvent = InputElement.PointerPressedEvent,
MouseButton = MouseButton.Left,
});
items.RemoveAt(1);
var panel = target.Presenter.Panel;
Assert.Null(KeyboardNavigation.GetTabOnceActiveElement((InputElement)panel));
}
示例11: Nested_ListBox_Does_Not_Change_Parent_SelectedIndex
public void Nested_ListBox_Does_Not_Change_Parent_SelectedIndex()
{
SelectingItemsControl nested;
var root = new SelectingItemsControl
{
Template = Template(),
Items = new IControl[]
{
new Border(),
nested = new ListBox
{
Template = Template(),
Items = new[] { "foo", "bar" },
SelectedIndex = 1,
}
},
SelectedIndex = 0,
};
root.ApplyTemplate();
root.Presenter.ApplyTemplate();
nested.ApplyTemplate();
nested.Presenter.ApplyTemplate();
Assert.Equal(0, root.SelectedIndex);
Assert.Equal(1, nested.SelectedIndex);
nested.SelectedIndex = 0;
Assert.Equal(0, root.SelectedIndex);
}
示例12: ApplyTemplate
private void ApplyTemplate(ListBox target)
{
// Apply the template to the ListBox itself.
target.ApplyTemplate();
// Then to its inner ScrollViewer.
var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
scrollViewer.ApplyTemplate();
// Then make the ScrollViewer create its child.
((ContentPresenter)scrollViewer.Presenter).UpdateChild();
// Now the ItemsPresenter should be reigstered, so apply its template.
target.Presenter.ApplyTemplate();
}