标签: selecteditem

如何以已编程方式将焦点设置为已具有焦点的WPF ListBox中的SelectedItem?

我们要设定SelectedItemListBox程序,并希望该项目则有焦点,使方向键的作用相对于选定的项目.看起来很简单.

然而问题是,如果在以编程方式ListBox设置时已经具有键盘焦点SelectedItem,而它正确地更新了该IsSelected属性ListBoxItem,则它不会将键盘焦点设置到它,因此,箭头键相对于之前关注的项目移动列表而不是人们所期望的新选择的项目.

这对用户来说非常混乱,因为它使选择似乎在使用键盘时跳转,因为它快速回到编程选择发生之前的位置.

注意:正如我所说,只有SelectedItem在以编程方式在ListBox已经具有键盘焦点的属性上设置属性时才会发生这种情况.如果它没有(或者如果它没有,你会离开,然后再回来),当键盘焦点返回时ListBox,正确的项目现在将按预期进行键盘焦点.

这是一些显示此问题的示例代码.要进行演示,请运行代码,使用鼠标在列表中选择"Seven"(从而将焦点放在其上ListBox),然后单击"Test"按钮.最后,点击键盘上的"Alt"键以显示焦点rect.您将看到它仍然实际上在'Seven'上,如果您使用向上和向下箭头,它们是相对于该行,而不是用户期望的'四'.

请注意,我已将按钮Focusable设置为false在按下按钮时不抢夺焦点列表框.如果我没有这个,ListBox当你点击按钮时会失去焦点,因此,当焦点返回到ListBox时,它将在正确的项目上.

XAML文件:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="525" Height="350" WindowStartupLocation="CenterScreen"
    Title="MainWindow" x:Name="Root">

    <DockPanel>

        <Button Content="Test"
            DockPanel.Dock="Bottom"
            HorizontalAlignment="Left"
            Focusable="False"
            Click="Button_Click" />

        <ListBox x:Name="MainListBox" />

    </DockPanel>

</Window>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

using System.Collections.ObjectModel;
using System.Windows;

namespace Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MainListBox.ItemsSource = new string[]{ …
Run Code Online (Sandbox Code Playgroud)

wpf listbox selecteditem

34
推荐指数
1
解决办法
5万
查看次数

WPF绑定到Listbox selectedItem

任何人都可以帮助以下 - 一直在玩这个,但不能为我的生活让它工作.

我有一个包含以下属性的视图模型;

public ObservableCollection<Rule> Rules { get; set; }
public Rule SelectedRule { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的XAML中,我得到了;

<ListBox x:Name="lbRules" ItemsSource="{Binding Path=Rules}" 
         SelectedItem="{Binding Path=SelectedRule, Mode=TwoWay}">
<ListBox.ItemTemplate>
    <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name:" />
                <TextBox x:Name="ruleName">
                    <TextBox.Text>
                        <Binding Path="Name" UpdateSourceTrigger="PropertyChanged" />
                    </TextBox.Text>
                </TextBox>
            </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

现在ItemsSource工作正常,我得到一个Rule对象列表,其名称显示在lbRules中.

我遇到的麻烦是将SelectedRule属性绑定到lbRules的SelectedItem.我尝试将textblock的text属性绑定到SelectedRule,但它始终为null.

<TextBlock Text="{Binding Path=SelectedRule.Name}" />
Run Code Online (Sandbox Code Playgroud)

我在输出窗口中看到的错误是:BindingExpression路径错误:找不到'SelectedRule'属性.

任何人都可以帮助我这个绑定 - 我不明白为什么它不应该找到SelectedRule属性.

然后我尝试将textblock的text属性更改为bellow,这有效.麻烦的是我想在我的ViewModel中使用SelectedRule.

<TextBlock Text="{Binding ElementName=lbRules, Path=SelectedItem.Name}" />
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助.

wpf binding listbox selecteditem

30
推荐指数
2
解决办法
11万
查看次数

在仅包含文本条目的组合框中获取所选文本的最简单方法是什么?

我的WPF ComboBox仅包含文本条目.用户将选择一个.获取所选ComboBoxItem文本的最简单方法是什么?请在C#和Visual Basic中回答.这是我的ComboBox:

<ComboBox Name="cboPickOne">
    <ComboBoxItem>This</ComboBoxItem>
    <ComboBoxItem>should be</ComboBoxItem>
    <ComboBoxItem>easier!</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我知道答案,但要找到它并不容易.我以为我会发布问题来帮助别人.修订:我学到了更好的答案.通过添加SelectedValuePath ="Content"作为ComboBox属性,我不再需要丑陋的强制转换代码.请参阅下面的安迪回答.

wpf combobox selectedvalue selecteditem selectedtext

29
推荐指数
4
解决办法
6万
查看次数

QML Listview选中项目突出显示单击

嗨,我想把这段代码:

highlight: Rectangle {
    color: "black"
    radius: 5 
    opacity: 0.7
    focus: true
}
Run Code Online (Sandbox Code Playgroud)

在onclick处理程序中进入mouseArea:

MouseArea {
    id: mouse_area1
    z: 1
    hoverEnabled: false
    anchors.fill: parent
    onClicked: {
    }
Run Code Online (Sandbox Code Playgroud)

这都是listView:

ListView {
         id: listview1
         x: 0
         y: 82
        // width: 574
        // height: 967
         width: window.width
         height: window.height
         visible: true
         keyNavigationWraps: false
         boundsBehavior: Flickable.DragAndOvershootBounds
         opacity: 1
         maximumFlickVelocity: 2500
         anchors.leftMargin: 0
         highlightMoveSpeed: 489
         contentWidth: 0
         preferredHighlightEnd: 2
         spacing: 5
         highlightRangeMode: ListView.NoHighlightRange
         snapMode: ListView.SnapToItem
         anchors.bottomMargin: 0
         anchors.rightMargin: 0
         anchors.topMargin: 82
              anchors.fill: parent
              model: myModel …
Run Code Online (Sandbox Code Playgroud)

qt listview selecteditem qml

25
推荐指数
3
解决办法
5万
查看次数

ComboBox ItemsSource已更改=> SelectedItem已毁坏

好吧,这一直困扰着我一段时间.我想知道其他人如何处理以下情况:

<ComboBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding SelectedItem}"/>
Run Code Online (Sandbox Code Playgroud)

DataContext对象的代码:

public ObservableCollection<MyItem> MyItems { get; set; }
public MyItem SelectedItem { get; set; }

public void RefreshMyItems()
{
    MyItems.Clear();
    foreach(var myItem in LoadItems()) MyItems.Add(myItem);
}

public class MyItem
{
    public int Id { get; set; }
    public override bool Equals(object obj)
    {
        return this.Id == ((MyItem)obj).Id;
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,当RefreshMyItems()调用该方法时,组合框会收到Collection Changed事件,更新其项目并且在刷新的集合中找不到SelectedItem =>将SelectedItem设置为null.但我需要组合框使用Equals方法来选择新集合中的正确项目.

换句话说 - ItemsSource集合仍然包含正确的MyItem,但它是一个new对象.而且我希望组合框能够使用类似的东西Equals来自动选择它(这更加困难,因为首先是源集合调用Clear()重置集合并且此时已将SelectedItem设置为null).

更新2在复制粘贴下面的代码之前,请注意它远非完美!请注意,默认情况下它不会绑定两种方式.

更新 …

collections wpf combobox selecteditem

23
推荐指数
3
解决办法
3万
查看次数

WPF选项卡控件:如何获取当前选定的选项卡?

在我的选项卡SelectionChanged事件中(这是正确的事件,我找不到选项卡更改事件?),如何访问新选项卡?

此外,在正常代码的此事件之外,如何访问当前选定的选项卡?

  TabControl.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(TabControl_SelectionChanged);

  void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  {
      //How so access my currently selected tab???
  }
Run Code Online (Sandbox Code Playgroud)

.net c# wpf tabcontrol selecteditem

21
推荐指数
3
解决办法
5万
查看次数

在WPF ListView中以编程方式选择项目

我无法弄清楚如何在ListView中以编程方式选择项目.

我正在尝试使用listview的ItemContainerGenerator,但它似乎不起作用.例如,在以下操作之后obj为null:

//VariableList is derived from BindingList
m_VariableList = getVariableList();
lstVariable_Selected.ItemsSource = m_VariableList;
var obj = 
    lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]);
Run Code Online (Sandbox Code Playgroud)

我已经尝试过(基于此处和其他地方的建议)使用ItemContainerGenerator的StatusChanged事件,但无济于事.事件永远不会发生.例如:

m_VariableList = getVariableList();
lstVariable_Selected.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
lstVariable_Selected.ItemsSource = m_VariableList;

...

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    //This code never gets called
    var obj = lstVariable_Selected.ItemContainerGenerator.ContainerFromItem(m_VariableList[0]);
}
Run Code Online (Sandbox Code Playgroud)

这件事的关键在于我只想预先选择ListView中的一些项目.

为了不留下任何东西,ListView使用了一些模板和拖放功能,所以我在这里包含了XAML.本质上,此模板使每个项目成为带有一些文本的文本框 - 当选择任何项目时,将选中该复选框.并且每个项目下面都会有一个小字形以插入新项目(这一切都正常):

<DataTemplate x:Key="ItemDataTemplate_Variable">
<StackPanel>
    <CheckBox x:Name="checkbox"
        Content="{Binding Path=ListBoxDisplayName}"
        IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
    <Image ToolTip="Insert Custom Variable" Source="..\..\Resources\Arrow_Right.gif" 
        HorizontalAlignment="Left" 
        MouseLeftButtonDown="OnInsertCustomVariable"
        Cursor="Hand" Margin="1, 0, 0, 2" Uid="{Binding Path=CmiOrder}" />
</StackPanel>
</DataTemplate> …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf listview selecteditem

20
推荐指数
2
解决办法
5万
查看次数

WPF ComboBox SelectedItem - 更改为以前的值

我有一个ComboBox,其SelectedItem绑定到ViewModel.

<ComboBox SelectedItem="{Binding SelItem, Mode=TwoWay}" ItemsSource="{Binding MyItems}">
Run Code Online (Sandbox Code Playgroud)

当用户在View ComboBox中选择一个新项目时,我想显示一个提示并确认他们想要进行更改.

在视图模型的SetItem属性设置器中,我显示一个对话框以确认选择.当他们说是的时候,它运作正常.

我的问题是,当用户点击"否"时,我不确定是谁让ComboBox恢复到之前的值.ViewModel中的Property具有正确的旧值,但在View中,ComboBox显示新选择的值.

我希望用户选择一个项目,确认他们要继续使用它,如果他们决定不这样做,我希望ComboBox恢复到上一个​​项目.

我怎么能做到这一点?谢谢!

wpf combobox selecteditem

20
推荐指数
2
解决办法
1万
查看次数

WPF Datagrid设置选定的行

如何使用Datagrid.SelectedItem以编程方式选择行?

我首先要创建一个IEnumerableDataGridRow对象和匹配的行传递给此SelectedItem属性或我该怎么办呢?

编辑:

TextBox.Text在选择行之前,我需要将第一列单元格的单元格内容与第一列匹配.

wpf select datagrid selecteditem

17
推荐指数
4
解决办法
10万
查看次数

使用MVVM获取选定的TreeViewItem

所以有人建议使用WPF TreeView,我想:"是的,这似乎是正确的方法." 现在,几个小时后,我简直无法相信使用这个控件有多困难.通过一系列研究,我能够使TreeView`控件正常工作,但我找不到"正确"的方法来将所选项目添加到视图模型中.我不需要从代码中设置所选项目; 我只需要我的视图模型就可以知道用户选择了哪个项目.

到目前为止,我有这个XAML,它本身不是很直观.这都在UserControl.Resources标记内:

<CollectionViewSource x:Key="cvs" Source="{Binding ApplicationServers}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="DeploymentEnvironment"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

<!-- Our leaf nodes (server names) -->
<DataTemplate x:Key="serverTemplate">
    <TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>

<!-- Note: The Items path refers to the items in the CollectionViewSource group (our servers).
           The Name path refers to the group name. -->
<HierarchicalDataTemplate x:Key="categoryTemplate"
                          ItemsSource="{Binding Path=Items}"
                          ItemTemplate="{StaticResource serverTemplate}">
    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)

这是树视图:

<TreeView DockPanel.Dock="Bottom" ItemsSource="{Binding Source={StaticResource cvs}, Path=Groups}"
              ItemTemplate="{StaticResource categoryTemplate}">
            <Style TargetType="TreeViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected}"/>
            </Style>
        </TreeView> …
Run Code Online (Sandbox Code Playgroud)

wpf treeview selecteditem mvvm

16
推荐指数
2
解决办法
3万
查看次数