我有一个绑定到数据表的数据绑定多选列表框.当我选择listboxitem时,我希望自动选择同一列表框中的其他listboxitems.我希望只需点击一下即可选择多个项目.我怎样才能做到这一点?我不能在SelectionChanged事件中这样做,因为它导致再次调用同一事件并完全打破我的逻辑.
请帮忙.任何帮助将受到高度赞赏.
更新:
我的列表框已绑定到具有IsSelected列的数据表.我在样式设置器中使用此列的值来选择listboxitem.假设我在datatable中有10行.如果用户选择第二个listboxitem,我可以将数据库中的对应行的isselected选为1.
但是我怎样才能同时选择其他项目呢?我认为肯特说,我宁愿使用属性进行绑定.但是,如何使用属性将列表框绑定到数据表?
我有一个记录,我想为添加到列表框的每个项目存储.我是否需要让记录成为一个类来完成这个?
TServerRec = record
ID: integer;
DisplayName: string;
Address: string;
Port: integer;
end;
procedure TMainForm.PopuplateServers;
var
server: TServerRec;
begin
for server in FServerList do
begin
lbServers.AddObject(server.DisplayName, server);
end;
end;
Run Code Online (Sandbox Code Playgroud) 我的 ListBox 项样式有问题,我创建了两种样式,但不知道如何一起使用。第一种样式用于 ListBox 项目大小、鼠标悬停颜色等,第二种样式用于项目背景(交替计数)。如果我离开其中一个,它们可以正常工作,但是如何使它们协同工作?或者也许我可以用一种风格来写?
我的代码是:
..... <Style x:Key="Style2"
TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
Name="Border"
Padding="7"
SnapsToDevicePixels="True">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background"
Value="{StaticResource SelectedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFFFFF"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#F7F7F7"></Setter>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="{x:Type ListBoxItem}"
TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource Style2}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter …Run Code Online (Sandbox Code Playgroud) 背景:我正在创建一个自定义列表框,每个列表框项上都有单选按钮,所以基本上它将是一个RadioButtonList.控件完全由代码创建.截至目前,控件呈现并正确运行并支持2个方向(水平/垂直).列表框使用ItemTemplate,它是一个带有RadioButton和TextBlock的StackPanel.
到目前为止,我已经能够通过使用将其背景设置为透明的样式来防止项目的背景颜色在选择项目时更改.
我也想对前景色做同样的事情.
基本上,ListBox的Selection模式是单一的,当选择一个项目时,我只希望它被RadioButton反射.
我使用以下代码来设置ItemContainerStyle:
System.Windows.Style style =
new System.Windows.Style(typeof(System.Windows.Controls.ListBoxItem));
System.Windows.Media.SolidColorBrush brush =
new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Transparent);
style.Resources.Add(System.Windows.SystemColors.HighlightBrushKey, brush);
Run Code Online (Sandbox Code Playgroud)
我的模板的TextBlock是使用System.Windows.FactoryFrameworkElement创建的,如下所示:
System.Windows.FrameworkElementFactory factoryTextBlock =
new System.Windows.FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock));
factoryTextBlock.SetBinding(System.Windows.Controls.TextBlock.TextProperty, new System.Windows.Data.Binding("Description"));
factoryStackPanel.AppendChild(factoryTextBlock);
Run Code Online (Sandbox Code Playgroud)
然后将FactoryTextBox附加到FactoryStackPanel并设置为ListBox的ItemTemplate.
目前,我在选择项目时将背景颜色设置为透明.由于文本默认设置为白色,因此在选择项目时它会直观消失.我正在寻找一种方法,在选择文本块的前景时设置颜色.现在它可以是黑色,但最终它会引用更高级别的字体颜色.
我正在尝试设置一个ListBox,以便每个项目都有一个文本块和一个组合框,在列表框的宽度上均匀分割,但我似乎无法找到ColumnDefinition属性的神奇组合来完成它.这是我的列表框项的DataTemplate.无论如何,我已经把它清理干净了.
<DataTemplate x:Key="MyDataTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding XPath=text()}"/>
<ComboBox Grid.Column="1" SelectedIndex="0" ItemsSource="{Binding Source={StaticResource Names}, XPath=Name}"></ComboBox>
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我已经在窗口上测试了一个带网格的简单应用程序.简单地指定两个ColumnDefinitions会使它们自动占据宽度的一半,这很好,但是当在listboxitem datatemplate中执行相同操作时,行为是不同的.
我如何更改datatemplate以使其工作?
谢谢!
我在WPF中创建了一个ListBox样式,以便它呈现为一个复选框列表.
当我手动填充ListBox的项目时,样式工作完美.但是,当我将ListBox的ItemsSource绑定到静态资源(包含所需项目的ItemsControl)时,样式将被完全删除.
这是风格:
<Style x:Key="CheckBoxListStyle" TargetType="ListBox">
<Style.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
<ContentPresenter
Grid.Column="1"
Margin="2,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
Run Code Online (Sandbox Code Playgroud)
这是ListBox的代码,它正确显示了样式:
<ListBox x:Name="ColumnsList"
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource CheckBoxListStyle}"
BorderThickness="1">
<ListBox.Items>
<ListBoxItem>Test</ListBoxItem>
<ListBoxItem>Test2</ListBoxItem>
<ListBoxItem>Test3</ListBoxItem>
</ListBox.Items>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
这是ListBox忽略样式的代码:
<ListBox x:Name="ColumnsList2"
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource CheckBoxListStyle}"
BorderThickness="1" …Run Code Online (Sandbox Code Playgroud) 我仍然没有得到它.你能否告诉我如何覆盖ListBox的默认行为.每次选择ListBoxItem时,都应更改Border的背景.不是整行的背景,而是仅指定边框的背景.
<ListBox ItemsSource="{Binding Source={StaticResource AssetsViewSource}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="Black">
<StackPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我有一个ListBox,它使用DataTemplate显示对象.DataTemplate包含一个TextBox.当用户选择ListBox中的项目时,我想将焦点设置为所选项目的TextBox.
我已经能够通过处理ListBox.SelectionChanged来部分实现这一点,但它仅在用户单击ListBox以选择项时才有效 - 如果用户选中ListBox并使用箭头键选择项,则它不起作用即使TextBox.Focus()被调用.
当用户使用键盘选择项目时,如何将焦点设置到TextBox?
这是ListBox的标记:
<ListBox Name="lb1" SelectionChanged="ListBox_SelectionChanged" ItemsSource="{Binding Items}" >
<ListBox.ItemTemplate>
<DataTemplate >
<TextBox></TextBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
这是处理代码:
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem lbi = (ListBoxItem)this.lb1.ItemContainerGenerator.ContainerFromItem(this.lb1.SelectedItem);
Visual v = GetDescendantByType<TextBox>(lbi);
TextBox tb = (TextBox)v;
tb.Focus();
}
Run Code Online (Sandbox Code Playgroud) 如何根据整个ListBox和可见的ScrollViewer区域获取ListBoxItem(它的左上角)的绝对(X,Y)位置?看起来WP7下没有TranslatePoint().
我确信这很简单,但我似乎无法弄明白.基本上,我试图强迫ListBoxItem自己超出ListBox宽度.例如,假设我们有粗ListBox有TextBox每个ListBoxItem:
<ListBox HorizontalContentAlignment="Stretch">
<ListBoxItem>
<TextBox TextWrapping="Wrap"/>
</ListBoxItem>
<ListBoxItem>
<TextBox TextWrapping="Wrap"/>
</ListBoxItem>
<ListBoxItem>
<TextBox TextWrapping="Wrap"/>
</ListBoxItem>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
如果你要输入它们,那么它TextWrapping设置的文本Wrap将继续向右流动,作为宽度ListBoxItem和ListBox调整到内容的宽度(注意HorizontalScrollBar出现):

我确定这是一个预期的行为,可能是由ScrollViewer模板内部引起的,但我希望文本包装并包含在原始宽度内.我可以通过设置一个静态宽度来解决这个问题,但这将是一个粗略的设计决定,而不是我想要做的事情(它会限制事物很容易重新调整大小).
基本上,我正在寻求的行为是ItemsControl:
<ItemsControl>
<TextBox TextWrapping="Wrap"/>
<TextBox TextWrapping="Wrap"/>
<TextBox TextWrapping="Wrap"/>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

我只是想保持ListBox控制,因为它需要我的其他行为.有任何想法吗?
listboxitem ×10
wpf ×8
listbox ×6
c# ×3
datatemplate ×2
styles ×2
xaml ×2
delphi ×1
focus ×1
itemscontrol ×1
silverlight ×1
styling ×1