在我的應用程序中,我有一個包裹在ScrollViewer中的畫布(畫布大於屏幕大小)。在這個畫布上,我放置了其他控件。畫布上的一個元素包含一個虛擬化的DataGrid(具有2000多行......所以它也涉及滾動)。現在我有一個函數根據行元素的某些值(觸發自動)在DataGrid中選擇一行。當選擇行時,我打電話ScrollIntoView與嵌套ScrollViewer
uxDataGrid.ScrollIntoView(uxDataGrid.SelectedItems[0]);
什麼工作完美罰款。事實上,這是一種行之有效的方式。我想要的是,DataGrid中的元素被選中,然後DataGrid應該滾動到正確的位置。但是我的Canvas滾動查看器也是以某種方式提取這個請求,並且在那裏滾動。
我已經試過攔截ScrollChangedEvent並設置了處理標誌。但它不起作用。
Qustions:
1)我怎麼能限制ScrollIntoView調用本地用戶控制的效果。
2)如果這是不可能的,我該如何做自己的滾動?我需要計算scrollviewer的垂直偏移量,但由於它是虛擬化的,我不知道如何找出行高度?
有什麼建議嗎?
我添加了一個快速示例來演示主要設置。當按下其中一個按鈕時,我只需要DataGrid滾動,而不是滾動顯示在畫布上。
<Window x:Class="ScrollTest.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">
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
<Button Click="Button1_Click">Scroll to 1</Button>
<Button Click="Button100_Click">Scroll to 100</Button>
<Button Click="Button200_Click">Scroll to 200</Button>
</StackPanel>
<ScrollViewer>
<Canvas Width="5000" Height="5000">
<DataGrid Name="uxDataGrid" ItemsSource="{Binding TestItems}" Width="500" Height="500"></DataGrid>
</Canvas>
</ScrollViewer>
</DockPanel>
public class TestItem
{
public TestItem(int id)
{
Property1 = id.ToString();
Property2 = "B";
}
public string Property1 { get; set; }
public string Property2 { get; set; }
}
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public IList<TestItem> TestItems { get; set; }
public MainWindow()
{
TestItems = new List<TestItem>();
for (int i = 0; i < 300; i++)
{
TestItems.Add(new TestItem(i));
}
InitializeComponent();
DataContext = this;
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
uxDataGrid.ScrollIntoView(TestItems[0]);
}
private void Button100_Click(object sender, RoutedEventArgs e)
{
uxDataGrid.ScrollIntoView(TestItems[99]);
}
private void Button200_Click(object sender, RoutedEventArgs e)
{
uxDataGrid.ScrollIntoView(TestItems[199]);
}
}
您能否將您的答案標記爲解決方案? –
尚未...我必須等待2天 – harri
謝謝。它很棒! –