WPF的ListBox和ListView有什么区别?我发现他们的属性没有任何显着差异.有不同的典型用途吗?
我正在使用此代码段来分析我在数据网格上选择的行.
for (int i = 0; i < dgDetalle.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dgDetalle.ItemContainerGenerator.ContainerFromIndex(i);
FrameworkElement cellContent = dgDetalle.Columns[0].GetCellContent(row);
// ... code ...
}
Run Code Online (Sandbox Code Playgroud)
循环运行平稳,但在处理某些索引时,第二行会抛出一个空异常.MSDN的文档说ItemContainerGenerator.ContainerFromIndex(i)如果'如果项目没有实现'将返回null,但这无助于我猜测如何获得所需的值.
如何扫描所有行?还有其他方法吗?
UPDATE
我正在使用这个片段来阅读这里CheckBox解释的内容.所以除非我改变很多东西,否则我不能使用绑定或根本不使用绑定.我不能.我正在进行代码维护.ItemSource
我已经阅读了关于这个主题的几个主题,但找不到任何可以做我想做的事情.我有一个树视图绑定到一组分层的对象.这些对象中的每一个都代表地图上的图标.当用户单击地图上的其中一个图标时,我想在树状视图中选择项目,将焦点放在其上,然后将其滚动到视图中.map对象具有绑定到treeview的对象列表.在示例中,Thing是绑定到树的对象的类型.
public void ScrollIntoView(Thing t)
{
if (t != null)
{
t.IsSelected = true;
t.IsExpanded = true;
TreeViewItem container = (TreeViewItem)(masterTreeView
.ItemContainerGenerator.ContainerFromItem(t));
if (container != null)
{
container.Focus();
LogicalTreeHelper.BringIntoView(container);
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,无论我尝试过什么,容器总是为空.有任何想法吗?
据我所知,调度程序发生在另一个线程中,它负责更新数据绑定,布局等.但是,有没有办法等到调度程序没有更多的项目或至少没有更多的数据绑定?我想确保属性更改已更新其所有组件,并在运行更多代码之前运行依赖属性更改的回调.
编辑:所以我猜这不是必需的,我只是想了解我应该做些什么.我的主要问题是WPF,如果滚动查看器的子项调整大小,滚动查看器会自动更新其范围吗?
但我也很好奇我是否可以等待绑定更新,或者是否有任何保证一个绑定在另一个之前更新?我是否希望编码以使绑定更新的顺序无关紧要?目前我使用依赖属性更改回调来执行各种通常依赖于其他属性更新的东西
所有,我试图循环DataGrid使用每个循环的WPF 来改变错误单元格的背景颜色.我检查了很多问题,但我还没有找到足够的答案.到目前为止我所拥有的是什么
public void RunChecks()
{
const int baseColumnCount = 3;
foreach (DataRowView rv in dataGrid.Items)
{
for (int i = baseColumnCount; i < dataGrid.Columns.Count; i++)
{
if (!CheckForBalancedParentheses(rv.Row[i].ToString()))
{
Color color = (Color)ColorConverter.ConvertFromString("#FF0000");
row.Background = new SolidColorBrush(color); // Problem!
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,为了改变Background我的行的颜色,DataGrid我需要使用与之相关的DataGridRow对象DataRowView rv.
如何获取DataGridRow对象rv(DataRowView)的引用?
谢谢你的时间.
编辑.基于下面的建议,我现在有以下样式,它与鼠标悬停在事件上并设置相关单元格的后退和前导字体.但是,我真的迷失了如何在上面的代码中在运行时将backcolor应用到单元格.XML风格是
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="FontWeight" Value="ExtraBold" …Run Code Online (Sandbox Code Playgroud) 我最近一直在使用WPF树视图,当用户使用在后备对象上设置IsSelected属性的搜索功能时,我正试图让所选项目显示在屏幕上时非常糟糕.
目前我的方法是使用这个答案中的方法:https://stackoverflow.com/a/34620549/800318
private void FocusTreeViewNode(TreeViewEntry node)
{
if (node == null) return;
var nodes = (IEnumerable<TreeViewEntry>)LeftSide_TreeView.ItemsSource;
if (nodes == null) return;
var stack = new Stack<TreeViewEntry>();
stack.Push(node);
var parent = node.Parent;
while (parent != null)
{
stack.Push(parent);
parent = parent.Parent;
}
var generator = LeftSide_TreeView.ItemContainerGenerator;
while (stack.Count > 0)
{
var dequeue = stack.Pop();
LeftSide_TreeView.UpdateLayout();
var treeViewItem = (TreeViewItem)generator.ContainerFromItem(dequeue);
if (stack.Count > 0)
{
treeViewItem.IsExpanded = true;
}
else
{
if (treeViewItem == null)
{
//This is …Run Code Online (Sandbox Code Playgroud) 我想将焦点放在数据网格的第一行.
这是我到目前为止:
Keyboard.Focus(ResultsGrid)
If result.Count > 0 Then
ResultsGrid.SelectedIndex = 0
End If
Run Code Online (Sandbox Code Playgroud)
这会将焦点设置为datagrid,而不是行本身.
我试着像这样排:
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;
Run Code Online (Sandbox Code Playgroud)
但我只有null.还有其他解决方案吗?我究竟做错了什么?
我想从我的细胞中获取数据.我的单元格是复选框.
我在这个问题上遇到了类似的问题,但是VirtualizingStackPanel.IsVirtualizing="False"没有解决我的问题.有没有人面临同样的问题?
问题是我有一个自定义组合框,
<Style TargetType="{x:Type MultiSelectionComboBox}" >
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"
VerticalAlignment="Center"
HorizontalAlignment="Center"
VirtualizingStackPanel.IsVirtualizing="False"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="ItemStack" VirtualizingStackPanel.IsVirtualizing="False">
<CheckBox x:Name="CheckBoxItem"
Command="{Binding SelectItem, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}"
CommandParameter="{Binding Key}"
>
</CheckBox>
<TextBlock Text="{Binding DisplayText}"></TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="Placement" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border BorderThickness="1" BorderBrush="Black">
<TextBox IsReadOnly="True" Grid.Column="0"
Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}">
</TextBox>
</Border> …Run Code Online (Sandbox Code Playgroud) wpf ×9
c# ×7
datagrid ×3
.net ×2
treeview ×2
combobox ×1
datarow ×1
null ×1
row ×1
wpf-controls ×1
wpfdatagrid ×1
wpftoolkit ×1
xaml ×1