所以我使用这个方式封装了一个ListBox控件,目前基本上没什么功能,就只是在每个ListBoxItem前面加上一个CheckBox。 以前介绍过如何自定义ItemsControl,要自定义一个ListBox控件,同样需要三部: 定义ListBox 关联ListBoxItem和ListBox 实现ListBox的逻辑 public class ExtendedListBoxItem也模仿UWP提供了MultiSelectEnabled和MultiSelectDisabled两个VisualState,因为ListBoxItem需要知道承载它的 ) listBoxItem.Owner = this; } ListBoxItem中使用监视Owner的IsMultiSelectCheckBoxEnabled和SelectionMode 在ListBoxItem里用Trigger比使用VisualState更简洁有效。 4.
="Center" Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem : queried Grid (hash=64665535) System.Windows.Data Warning: 73 : Lookup ancestor of type ListBoxItem : queried Grid (hash=22098140) System.Windows.Data Warning: 73 : Lookup ancestor of type ListBoxItem : queried Grid (hash=42455531) System.Windows.Data Warning: 73 : Lookup ancestor of type ListBoxItem : queried Grid (hash=2598608) System.Windows.Data Warning: 73 : Lookup ancestor of type ListBoxItem
<AutoSuggestBox Header="AutoSuggestBox" /> <TextBlock Text="ListBox" /> <ListBox> <ListBoxItem Content="ListBoxItem 1" /> <ListBoxItem Content="ListBoxItem 2" /> <ListBoxItem Content ="ListBoxItem 3" /> </ListBox> </StackPanel> ? Content="ListBoxItem 1" /> <ListBoxItem Content="ListBoxItem 2" /> <ListBoxItem Content="ListBoxItem 3" /> </ListBox> </local:HeaderedContentControl> </StackPanel> ?
<TabItem Header="HaHaTabItem" > <ListBox> <ListBoxItem Content="Blue" /> <ListBoxItem Content="Black" /> <ListBoxItem
I'd like to be able to set up the triggers in those files, looking at the ListBoxItem's IsSelected property The template to override the ListBoxItem follows: <Style TargetType="{x:Type <em>ListBoxItem</em>}"> Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type <em>ListBoxItem</em> DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem
以硬编码的形式填充数据,请看代码: <ListBox> <ListBoxItem>列表项1</ListBoxItem> <ListBoxItem>列表项2</ListBoxItem> <ListBoxItem>列表项3</ListBoxItem> <ListBoxItem>列表项4</ListBoxItem> <ListBoxItem>列表项5</ListBoxItem > </ListBox> 而且,ListBoxItem可以容纳控件,比如Button,CheckBox,都可以往里填充: <ListBox> <ListBoxItem> <Button Content="我是按钮"/> </ListBoxItem> <ListBoxItem> <CheckBox IsChecked="True"/> </ListBoxItem> </ListBox> 在这种情况下,ListBoxItem标签就可以省略了,系统会自动进行封装: <ListBox> <Button Content=
listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem; // 获取选择元素的位置 Point position = listBoxItem.TranslatePoint(new Point(0, 0), list); //滚动到指定位置 this.scroll.ScrollToVerticalOffset 这种方案是可行的,示例代码如下 Random r = new Random(); var item = this.list.Items[r.Next(0, 50)]; ListBoxItem listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem; // 获取选择元素的位置 Point position = listBoxItem.TranslatePoint(new Point(0, 0), list); var gap = position.Y - this.scroll.VerticalOffset
在列表新建一个图标,添加 Visibility Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem} Grid.ColumnDefinitions> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=<em>ListBoxItem</em> Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem ListView.ItemTemplate> </ListView> 获取当前列表项,使用{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem Grid.ColumnDefinitions> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=<em>ListBoxItem</em>
Width="100" Height="100" Margin="5" Content="1"/> <ListBoxItem Width="100" Height="100" Margin ="5" Content="2"/> <ListBoxItem Width="100" Height="100" Margin="5" Content="3"/> <ListBoxItem Width="100" Height="100" Margin="5" Content="4"/> <ListBoxItem Width="100" Height="100" Margin ="5" Content="5"/> <ListBoxItem Width="100" Height="100" Margin="5" Content="6"/> <ListBoxItem Width="100" Height="100" Margin="5" Content="7"/> <ListBoxItem Width="100" Height="100" Margin
ListBoxItem:ListBox中每一个项都是一个ListBoxItem,可以通过定义ListBoxItem的样式来自定义ListBox的外观。 --<ListBoxItem Content="软件班" IsSelected="True" ></ListBoxItem> <ListBoxItem Content="数据班" ></ ListBoxItem> <ListBoxItem Content="英语班" ></ListBoxItem>--> </ListBox> </Grid> private void
在这个事件中我们可以得到我们所画出的形状的区域及 e.Strokes[0].GetGeometry(),然后我们对这ListBox的这个区域做命中检查VisualTreeHelper.HitTest,从中过滤出我们想选中的多个ListBoxItem <ListBox Name="listBox1" SelectionMode="Extended" Height="350" Width="780"> <ListBoxItem Content="Ok" /> <ListBoxItem Content="Maxzhang" /> <ListBoxItem Content="houxiaodong" /> <ListBoxItem Content="hao1" /> <ListBoxItem Content="wuhao" /> <ListBoxItem Content="xiaoge" /> <ListBoxItem
在列表新建一个图标,添加 Visibility Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem} Grid.ColumnDefinitions> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=<em>ListBoxItem</em> Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem 获取当前列表项,使用{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex Grid.ColumnDefinitions> <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=<em>ListBoxItem</em>
= null) { ListBoxItem _selectedItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem MessageBox.Show(string.Format("选中行的TextBlock值为:" + myTxt.Text)); } ListBoxItem _firstItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items[0])); null; } } public class TestData{public string d{set;get;}} } 这里我们借助VisualTreeHelper对指定行(ListBoxItem
</StackPanel> </Border> </ControlTemplate> </ListBox.Template> <ListBoxItem Content="ListBoxItem 1" /> <ListBoxItem Content="ListBoxItem 2" /> <ListBoxItem Content="ListBoxItem
<CollectionContainer Collection="{Binding Source={StaticResource Items2Source}}" /> <ListBoxItem >Walterlv End Item 1</ListBoxItem> <ListBoxItem>Walterlv End Item 2</ListBoxItem>
ListBoxItem用于填充每一项列表数据。 要选择一个ListBoxItem在列表框,设置IsSelected属性true。 默认情况下,的HorizontalAlignment一个的ListBoxItem的设置为拉伸。StackPanel的默认水平位置是Center。 如果您通过StackPanel设置ListBoxItem的Width属性,则应用面板的默认值并且项目居中。
{ return new MyListBoxItem(); } } public class MyListBoxItem : ListBoxItem (dep is ListBoxItem)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { return; } ListBoxItem item = (ListBoxItem)dep;
</Setter.Value> 35 </Setter> 36 </Style> 37 38 39 40 <Style TargetType="<em>ListBoxItem</em> Property="Template"> 42 <Setter.Value> 43 <ControlTemplate TargetType="<em>ListBoxItem</em>
当然解决方案有很多: 可以写两个ListBoxItem的样式,第一个放顶部有渐变的背景,和右部保持一致,通过样式选择器来实现.这显然比较麻烦. 还可以在大背景下放个渐变,ListBoxItem的上半部分做成透明,这样相对简单,但不一定能实现理想的效果. IsHitTestVisible属性就很好的解决了这个问题.直接在上层放个border,背景设置成渐变,IsHitTestVisible设置为false.这样就既能看到渐变效果,又能透过border,直接点到ListBoxItem
ToggleButton • HyperlinkButton • CheckBox • RadioButton • ListBox • ListBoxItem • Custom BorderBrush/BorderThickness Change • ButtonBase Changes • ListBox and ListBoxItem