WPF学习问题汇集:
WPF中ItemsSource改变,DataGrid中不更新
需要将ItemsSource先赋值为null
,而后再赋值为新的值。
例如:
gridBeamInfo.ItemsSource = null;
if (beamInfoList.Count==0)
{
beamInfoList = new List<BeamInfo>();
beamInfoList.Add(new BeamInfo("未搜索到...", "无"));
}
gridBeamInfo.ItemsSource = beamInfoList;
WPF中DataGrid
高亮搜索符合项
不应遍历DataGrid的cell来搜索符合项,在设计 UI 控件时,开发者保证它与数据源(例如实现了 ObservableCollection
//仅供参考,不建议使用
for (int i = 0; i < gridBeamInfo.Items.Count; i++)
{
string beamName = (gridBeamInfo.Columns[0].GetCellContent(gridBeamInfo.Items[i]) as TextBlock).Text;
if (beamName == searchedBeamName)
{
gridBeamInfo.SelectedIndex = i;
gridBeamInfo.ScrollIntoView(gridBeamInfo.Items[i]);
//gridBeamInfo.Focus();
var searchedEle = beamInfoManager.BeamInfoDict[searchedBeamName];
EbDb.UIViewZoom(EbDb.ActivView2D.GView, searchedEle);
return;
}
}
正解:是通过绑定的数据源,遍历数据源,然后将数据源强制转换为object,调用ScrollIntoView方法和设定SelectedItem属性即可。
for (int i = 0; i < beamInfoList.Count; i++)
{
//var textblock = gridBeamInfo.Columns[0].GetCellContent(gridBeamInfo.Items[i]) as TextBlock; 禁止使用该方法遍历DataGrid控件的cell,会出现访问越界的异常
if (beamInfoList[i].beamName==searchedBeamName)
{
gridBeamInfo.SelectedItem =(object)beamInfoList[i];
gridBeamInfo.ScrollIntoView((object)beamInfoList[i]);
//gridBeamInfo.Focus();
var searchedEle = beamInfoManager.BeamInfoDict[searchedBeamName];
EbDb.UIViewZoom(EbDb.ActivView2D.GView, searchedEle);
return;
}
}
- 高亮被选择的Cell代码是
gridBeamInfo.SelectedIndex = i;
gridBeamInfo.ScrollIntoView(gridBeamInfo.Items[i]);
<Page x:Class="EBPlugIn2.BeamSYPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:EBPlugIn2" xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="190" Title="梁编号索引" Unloaded="Page_Unloaded" Loaded="Page_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBox x:Name="searchbox" TextChanged="textbox_TextChanged" Grid.Row="0" Height="26">
<TextBox.Resources>
<VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="输入编号快速定位" FontSize="12"/>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Resources>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource HintText}"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<DataGrid x:Name="gridBeamInfo" AutoGenerateColumns="False" IsReadOnly="True" Grid.Row="1" SelectionMode="Single" SelectionChanged="gridBeamInfo_SelectionChanged">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="RosyBrown"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="名称" Binding="{Binding beamName}" Width="2*"></DataGridTextColumn>
<DataGridTextColumn Header="数量" Binding="{Binding beamNum}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="2" Content="关闭对话框" Height="26" Click="Button_Click"/>
</Grid>
</Page>
WPF中无法访问到XXX
重新生成dll
#####
愿你一寸一寸地攻城略地,一点一点地焕然一新
#####