![](/img/trans.png)
[英]Binding a collectionviewsource in datagrid celltemplate
[英]DataGrid CellTemplate TabNavigation
我以為以前已經有人問過這個問題,但我找不到,所以讓我知道它是否與其他詞或其他詞重復。
問題出在SL4 DataGrid上,它包含多個CellTemplate,包括Checkbox
, Button
等。默認情況下,它將僅在第一行中瀏覽這些元素。 如果我設置TabNavigation="Cycle"
,它將在所有元素之間進行制表,但不會繼續移動到下一個元素,而是僅通過同一DataGrid重復該制表。
如果我將其設置為Once
再次”,它將僅跳至第一行。...並且SL4似乎沒有“ Continue
選項,一旦到達邊緣,便可以移動到下一個對象。
我正在尋找一種簡單的方法來獲取等同於TabNavigation="Cycle"
除了當它到達DataGrid
的最后一個可制表元素時,它會移動到樹中的下一個對象,而不僅僅是跳回到DataGrid
第一個元素。 我在這里想念什么?
在Silverlight中似乎沒有執行此操作的本地方法,這是數據網格控件中受支持的按鍵列表: http : //msdn.microsoft.com/zh-cn/library/cc838112(v= VS.95)的.aspx
我可以通過使用KeyDown事件並檢查Tab,然后手動設置編輯單元來偽造它:
<Grid x:Name="LayoutRoot" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Some text" />
<sdk:DataGrid Grid.Row="1" ItemsSource="{Binding People}" AutoGenerateColumns="False" KeyDown="DataGrid_KeyDown">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding FirstName}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding LastName}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<TextBox Grid.Row="2" Text="Some more text" />
</Grid>
private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
DataGrid dg = (DataGrid)sender;
ObservableCollection<Person> items = dg.ItemsSource as ObservableCollection<Person>;
if (e.Key == Key.Tab && dg.SelectedIndex < items.Count -1)
{
dg.SelectedIndex++;
dg.CurrentColumn = dg.Columns[0];
dg.BeginEdit();
var cell = dg.CurrentColumn.GetCellContent(dg.SelectedItem);
}
}
很久以前,我對SL4有一些了解。 我將嘗試您的問題:
看到您設置為獲得所需行為的屬性將不起作用。 這僅是Microsoft的方式,因此替代方法是編寫您自己的代碼以實現所需的行為。
我的想法是將以下事件附加到每個數據網格單元:
private void DataGridCell_KeyDown(object sender, KeyEventArgs e)
{
if (keypressed == 'TAB' && last cell of the datagrid)
{
e.handled=true;
int tabIndex = dg.TabIndex;
tabindex++;
Control control = GetControl(tabIndex); // You can use visual tree in the method to get it
control.select();
control.focus();
}
}
抱歉,我寫的是偽代碼,而不是真實的代碼,因為花時間來回憶起我以前在SL中執行的代碼。
希望當您從數據網格中跳出並反向進入數據網格時,此解決方案對您都有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.