Dostęp do ScrollViewer listy ListB z C #
Chciałbym zmienić właściwości aScrollViewer
aListBox
z C #.
znalazłemto pytanie tutaj na Stackoverflow. Wziąłem zaakceptowaną odpowiedź i odsłoniłemScrollViewer
jako własność podklasy. Jednak wydaje się, że nie działa to w poniższym przykładzie. Niektóre komentarze w tym pytaniu również stwierdzają, że ta technika nie działa.
XAML:
<code><Window x:Class="StackoverflowListBoxScrollViewer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> </Window> </code>
DO#:
<code>using System; using System.Windows; using System.Windows.Controls; namespace StackoverflowListBoxScrollViewer { public class MyListBox : ListBox { public ScrollViewer ScrollViewer { get { return (ScrollViewer)GetTemplateChild("ScrollViewer"); } } } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var myListBox = new MyListBox(); Content = myListBox; myListBox.Items.Add(new Button() { Content = "abc" }); myListBox.Items.Add(new Button() { Content = "abc" }); myListBox.Items.Add(new Button() { Content = "abc" }); myListBox.Items.Add(new Button() { Content = "abc" }); myListBox.Items.Add(new Button() { Content = "abc" }); var button = new Button() { Content = "Check ScrollViewer" }; button.Click += (s, e) => { if (myListBox.ScrollViewer == null) Console.WriteLine("null"); }; myListBox.Items.Add(button); } } } </code>
Po kliknięciu przycisku „Sprawdź ScrollViewer” drukuje „null”. To znaczyScrollViewer
nie został odzyskany.
Jak dostanę się do tej ceryScrollViewer
? :-)