标签: listbox

如何删除所有ListBox项目?

我创建了两个RadioButton(重量和高度).我会在两个类别之间切换.但是它们共享相同的ListBox控制器(listBox1和listBox2).

有没有什么好方法可以更简单地清除所有ListBox项?我找不到ListBox的removeAll().我不喜欢我在这里发布的复杂的多行样式.

private void Weight_Click(object sender, RoutedEventArgs e)
    {
        // switch between the radioButton "Weith" and "Height"
        // Clear all the items first
        listBox1.Items.Remove("foot"); 
        listBox1.Items.Remove("inch");
        listBox1.Items.Remove("meter");
        listBox2.Items.Remove("foot");
        listBox2.Items.Remove("inch");
        listBox2.Items.Remove("meter");

        // Add source units items for listBox1
        listBox1.Items.Add("kilogram");
        listBox1.Items.Add("pound");

        // Add target units items for listBox2
        listBox2.Items.Add("kilogram");
        listBox2.Items.Add("pound");
    }

    private void Height_Click(object sender, RoutedEventArgs e)
    {
        // switch between the radioButton "Weith" and "Height"
        // Clear all the items first
        listBox1.Items.Remove("kilogram");
        listBox1.Items.Remove("pound");
        listBox2.Items.Remove("kilogram");
        listBox2.Items.Remove("pound");

        // Add source units items for listBox1 …
Run Code Online (Sandbox Code Playgroud)

c# wpf listbox

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

强制数据绑定WPF ListBox更新的更好方法是什么?

我有一个绑定到ObservableCollection的WPF ListBox,当集合发生变化时,所有项目都会更新它们的位置.

新位置存储在集合中,但UI不会更新.所以我添加了以下内容:

    void scenarioItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ToolboxListItem.UpdatePositions();
        lstScenario.ItemsSource = null;
        lstScenario.ItemsSource = ToolboxListItem.ScenarioItems;
        this.lstScenario.SelectedIndex = e.NewStartingIndex;
    }
Run Code Online (Sandbox Code Playgroud)

通过将ItemsSource设置为null然后再次绑定它,UI将更新,

但这可能是非常糟糕的编码:p

建议?

data-binding wpf listbox observablecollection

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

如何基于属性值禁用数据绑定ListBox项?

有没有人知道是否以及如何ListBox根据属性的值禁用数据绑定中的项目?

我想要一个DataTrigger,当某个属性是false,禁用这个项目(使其无法选择)而不影响其中的其他项目ListBox.

<ListBox>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Name="textBlock" Text="{Binding Description}"/>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsEnabled}" Value="False">
          ??
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

wpf xaml listbox datatemplate

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

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万
查看次数

为什么ListBox不选择项目,但ListBox是?

我在视图中有以下代码:

<%= Html.ListBoxFor(c => c.Project.Categories,
        new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>

<%= Html.ListBox("MultiSelectList", 
        new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>
Run Code Online (Sandbox Code Playgroud)

唯一的区别是第一个帮助器是强类型的(ListBoxFor),并且它无法显示所选项(1,2),即使项目出现在列表中,等等.更简单的ListBox正在按预期工作.

我显然在这里遗漏了一些东西.我可以使用第二种方法,但这真的让我烦恼,我想弄清楚.

作为参考,我的模型是:

public class ProjectEditModel
{
    public Project Project { get; set; }
    public IEnumerable<Project> Projects { get; set; }
    public IEnumerable<Client> Clients { get; set; }
    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Tag> Tags { get; set; }
    public ProjectSlide SelectedSlide { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

更新 …

asp.net-mvc listbox

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

scrollviewer的子元素阻止滚动鼠标滚轮?

我在使用鼠标滚轮在以下XAML中工作时遇到问题,为简化起见,我已简化了这一点:

<ScrollViewer
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
CanContentScroll="False"
>
    <Grid
    MouseDown="Editor_MouseDown"
    MouseUp="Editor_MouseUp"
    MouseMove="Editor_MouseMove"
    Focusable="False"
    >
        <Grid.Resources>
            <DataTemplate
            DataType="{x:Type local:DataFieldModel}"
            >
                <Grid
                Margin="0,2,2,2"
                >
                    <TextBox
                    Cursor="IBeam"
                    MouseDown="TextBox_MouseDown"
                    MouseUp="TextBox_MouseUp"
                    MouseMove="TextBox_MouseMove"
                    />
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <ListBox
        x:Name="DataFieldListBox"
        ItemsSource="{Binding GetDataFields}"
        SelectionMode="Extended"
        Background="Transparent"
        Focusable="False"
        >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style
                TargetType="ListBoxItem"
                >
                    <Setter
                    Property="Canvas.Left"
                    Value="{Binding dfX}"
                    />
                    <Setter
                    Property="Canvas.Top"
                    Value="{Binding dfY}"
                    />
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

在视觉上,结果是一些已知大小的区域,其中DataField从集合中读取的s可以用TextBox具有任意位置,大小等的es 来表示.如果ListBox"样式"区域"太大而无法一次显示",则可以进行水平和垂直滚动,但只能使用滚动条.

为了更好的人体工程学和理智,鼠标滚轮应该是可能的,并且通常ScrollViewer会自动处理它,但ListBox似乎是处理这些事件,使父母ScrollViewer永远不会看到它们.到目前为止,我只能够得到滚轮滚动的工作是设定IsHitTestVisible=False为无论是ListBox …

wpf listbox event-handling mousewheel scrollviewer

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

ItemContainerGenerator.ContainerFromItem如何使用分组列表?

我有一个ListBox,直到最近才显示一个项目的平面列表.我能够使用myList.ItemContainerGenerator.ConainerFromItem(thing)来检索列表中托管"thing"的ListBoxItem.

本周我稍微修改了ListBox,因为它为其项目绑定的CollectionViewSource已启用分组.现在ListBox中的项目被分组在好的标题下面.

但是,由于执行此操作,ItemContainerGenerator.ContainerFromItem已停止工作 - 即使对于我知道在ListBox中的项目,它也会返回null.Heck - 即使ListBox填充了很多项,ContainerFromIndex(0)也返回null!

如何从显示分组项的ListBox中检索ListBoxItem?

编辑:这是修剪示例的XAML和代码隐藏.这会引发NullReferenceException,因为即使列表中有四个项目,ContainerFromIndex(1)也会返回null.

XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="Window1">

    <Window.Resources>
        <XmlDataProvider x:Key="myTasks" XPath="Tasks/Task">
            <x:XData>
                <Tasks xmlns="">
                    <Task Name="Groceries" Type="Home"/>
                    <Task Name="Cleaning" Type="Home"/>
                    <Task Name="Coding" Type="Work"/>
                    <Task Name="Meetings" Type="Work"/>
                </Tasks>
            </x:XData>
        </XmlDataProvider>

        <CollectionViewSource x:Key="mySortedTasks" Source="{StaticResource myTasks}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="@Type" />
                <scm:SortDescription PropertyName="@Name" />
            </CollectionViewSource.SortDescriptions>

            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Type" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <ListBox 
        x:Name="listBox1" 
        ItemsSource="{Binding Source={StaticResource mySortedTasks}}" 
        DisplayMemberPath="@Name"
        >
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
    </ListBox>
</Window>
Run Code Online (Sandbox Code Playgroud)

CS:

public Window1() …
Run Code Online (Sandbox Code Playgroud)

.net wpf listbox

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

如何将上下文菜单添加到ListBoxItem?

我有一个ListBox,我想为列表中的每个项目添加一个上下文菜单.我已经看到"解决方案"右键单击选择一个项目并禁止上下文菜单,如果在空白区域,但这个解决方案感觉很脏.

有谁知道更好的方法?

c# listbox contextmenu winforms

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

ListBoxItem生成"System.Windows.Data Error:4"绑定错误

我创造了以下事件ListBox:

<ListBox x:Name="RecentItemsListBox" Grid.Row="1" BorderThickness="0" Margin="2,0,0,0" SelectionChanged="RecentItemsListBox_SelectionChanged">
  <ListBox.Resources>
      <Style TargetType="{x:Type ListBoxItem}"
             BasedOn="{StaticResource {x:Type ListBoxItem}}">
          <Style.Triggers>
              <!--This trigger is needed, because RelativeSource binding can only succeeds if the current ListBoxItem is already connected to its visual parent-->
              <Trigger Property="IsVisible" Value="True">
                  <Setter Property="HorizontalContentAlignment"
                          Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
                  <Setter Property="VerticalContentAlignment"
                          Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
              </Trigger>
          </Style.Triggers>
      </Style>
  </ListBox.Resources>
  <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,2,0,0">
                <TextBlock Text="{Binding Number}" />
                <StackPanel Orientation="Vertical" Margin="7,0,0,0">
                    <TextBlock Text="{Binding File}" />
                    <TextBlock Text="{Binding Dir}" …
Run Code Online (Sandbox Code Playgroud)

.net wpf xaml listbox listboxitem

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

如何在列表框中上下移动项目?

我有一个listBox1对象,它包含一些项目.我有一个按钮可以向上移动所选项目,另一个按钮可以向下移动所选项目.两个按钮的代码应该是什么?

.net c# listbox winforms

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