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 模板的集合)保持双向绑定(不但绑定内容记录,而且绑定集合的变化)。但是并不提供你去遍历控件的 Items 的机制! 在桌面的控件中,由于大量重复使用控件,因此控件占用的内存是个大问题。所以这类数据绑定控件通常会复用 Item 对应的控件。例如 DataGrid 可能只保持30行子控件,当你的数据有30000行时,另外29970行并不对应任何子控件。当最终用户在界面上上下滚动屏幕区域时,已经离开可见区域的子控件可能会触发一个类似 xxxxxUnload 的事件,随后就被重新用新数据绑定了(而不是销毁了),重新绑定之后就会再触发一个类似 xxxxxLoad 的事件。 也就是说,子控件的自动重用的。如果你遍历这些子控件,你只会得到较少的、绑定数据貌似混乱的结果。因为之后当前可见的那些子控件对象实例才保证绑定的数据是准确的,它们过了瞬间就会绑定到其它数据上! 所以要遍历DataGrid之类的控件所绑定的数据集合,那么就去遍历数据集合。千万不要去遍历其Items控件集合、在通过Item去访问绑定的数据对象,这是不会有稳定的和准确的结果的。

//仅供参考,不建议使用
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

##### 愿你一寸一寸地攻城略地,一点一点地焕然一新 #####
原文地址:https://www.cnblogs.com/johnyang/p/15239178.html