簡體   English   中英

最初關注 ItemsControl - DataTemplate

[英]Initial focus on the ItemsControl - DataTemplate

我有一個 WPF window 我在其中顯示一些位置信息。 這些位置來自 BindingList ( AvailableLocations ),並使用ScrollViewer中的DataTemplate顯示。 下面是我的 xaml 代碼。

<Border Margin="0" Background="White">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="Title" FontSize="14" FontWeight="Bold" Margin="10"/>
            <Button Content="X" HorizontalAlignment="Right" MinWidth="0" Grid.Column="1" Command="{Binding CancelCommand}" Margin="2.5" />
        </Grid>
        <Border Margin="10,0,10,10" BorderBrush="Black" BorderThickness="0.5" Grid.Row="1"  Background="GhostWhite">
            <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,2.5,2.5,2.5"  IsTabStop="False" Focusable="False">
                <ItemsControl ItemsSource="{Binding AvailableLocations}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button Content="{Binding Description}" Margin="0, 10" MinWidth="200">
                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <Border x:Name="Border" Width="{TemplateBinding Width}"
                                        Height="{TemplateBinding Height}"
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="Gray"
                                        BorderThickness="1">
                                            <Label x:Name="Content"  Content="{TemplateBinding Content}"
                                           FontWeight="{TemplateBinding FontWeight}"
                                           FontSize="{TemplateBinding FontSize}"/>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="Background" Value="LightGreen" TargetName="Border"/>
                                                <Setter Property="Cursor" Value="Hand"/>
                                            </Trigger>
                                            <Trigger Property="IsPressed" Value="True">
                                                <Setter Property="Background" Value="LightGreen" TargetName="Border"/>
                                                <Setter Property="BorderThickness" Value="2" TargetName="Border"/>
                                                <Setter Property="BorderBrush" Value="DarkGreen" TargetName="Border"/>
                                                <Setter Property="FontWeight" Value="SemiBold" TargetName="Content"/>
                                                <Setter Property="Cursor" Value="Hand"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Button.Template>
                            </Button>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </ScrollViewer>
        </Border>
        <Button Name="btnCancel" Content="_Cancel" Command="{Binding CancelCommand}" IsCancel="True" Grid.Row="2"  Margin="10" HorizontalAlignment="right"  MinWidth="70"/>
    </Grid>

</Border>

而我的ViewModel和Model如下

class Window2ViewModel
{
    public BindingList<Location> AvailableLocations { get; private set; }
    public Window2ViewModel()
    {
        IList<Location> locations = new List<Location>()
        {
            new Location(){Description ="Desc 1"},
            new Location(){Description ="Desc 2"},
            new Location(){Description ="Desc 3"},
            new Location(){Description ="Desc 4"},
            new Location(){Description ="Desc 5"},
        };

        AvailableLocations = new BindingList<Location>(locations);
    }
}
public class Location
{
    public string Description { get; set; }
    public double LocationCD { get; set; }
    public string LocationType { get; set; }
    public double LocationTypeCd { get; set; }
    public string LocationTypeMean { get; set; }
    public double OrganizationID { get; set; }

} 

我在這里面臨兩個問題。

  1. 當我做選項卡時,焦點轉到可用位置項目的容器,我想限制它。 我希望位置字段和按鈕可以選擇,而不是容器控件。 我嘗試將IsTabStop="False" Focusable="False"設置為ScrollViewer ,但這不起作用。
  2. 我希望 window 的初始焦點位於第一個位置字段(在本例中為 Desc 1)。 我知道對於 window 級別的控件,這可以通過 FocusManager ( FocusManager.FocusedElement="{Binding ElementName=controlName} 。但我不知道如何為DataTemplate中的項目完成。

希望你們能理解我面臨的問題,對此的任何幫助將不勝感激。

提前致謝

在做了很多試驗和錯誤之后,我意識到焦點既不是Border也不是ScrollViewer元素。 焦點實際上轉到ItemsControl元素。 因此,將IsTabStop = False設置為ItemsControl解決了我的第一個問題。

對於 DataTemplate 項的初始焦點,我找不到任何直接修復。 但是使用下面的代碼,我能夠將焦點放在我的 DataTemplate 中的第一個元素上。

Loaded += (s, e) =>
              {
                  foreach (var item in itemCtrl.Items)
                  {
                      UIElement uiElement =
                          (UIElement)itemCtrl.ItemContainerGenerator.ContainerFromItem(item);
                      if (uiElement != null)
                      {
                          if (uiElement is ContentPresenter)
                          {
                              ContentPresenter c = (uiElement as ContentPresenter);
                              Button b = c.ContentTemplate.FindName("btn", c) as Button;

                              Dispatcher.BeginInvoke(new Action(() =>
                              {
                                  b.Focus();
                              }), DispatcherPriority.Normal);

                              return;
                          }
                      }
                  }
              };

我不太確定上述是否可以直接在 xaml 內部完成。 希望有人會找到更合適的解決方案。 目前,上述修復對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM