DataGrid OnRender问题中的自定义控件

时间:2015-07-01 06:44:29

标签: c# wpf custom-controls

我正在使用这个例子:

https://code.msdn.microsoft.com/windowsdesktop/A-Searchable-Highlight-d7b911f3

为了利用我DataGrid中的项目突出显示。

SearchableTextControl来自Control

<logAnalyzer:SearchableTextControl 
                         IsHighlight="True" 
                         Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"
                         SearchText="{Binding ElementName=SearchTermTextBox, Path=Text, UpdateSourceTrigger=PropertyChanged}"
                         FontSize="15"
                         Margin="3,0"
                         IsMatchCase="False"/>

这是我正在设置TextSearchText属性的XAML。

当我加载Window时,一切正常。

enter image description here

如上图所示,ID 100的最后一列没有值,这对我的方案来说很好。

但是,如果我向下滚动一点,然后再次上升,那行就会以某种方式填充。

enter image description here

我认为问题来自OnRender覆盖方法:

    protected override void OnRender(DrawingContext drawingContext)
    {
        // Define a TextBlock to hold the search result.
        TextBlock displayTextBlock = this.Template.FindName("PART_TEXT", this) as TextBlock;

        if (string.IsNullOrEmpty(this.Text))
        {
            base.OnRender(drawingContext);

            return;
        }
        if (!this.IsHighlight)
        {
            displayTextBlock.Text = this.Text;
            base.OnRender(drawingContext);

            return;
        }

        displayTextBlock.Inlines.Clear();
        string searchstring = this.IsMatchCase ? (string)this.SearchText : ((string)this.SearchText).ToUpper();

        string compareText = this.IsMatchCase ? this.Text : this.Text.ToUpper();
        string displayText = this.Text;

        Run run = null;
        if (!string.IsNullOrEmpty(searchstring) && compareText.IndexOf(searchstring) >= 0)
        {
            int position = compareText.IndexOf(searchstring);
            run = GenerateRun(displayText.Substring(0, position), false);

            if (run != null)
            {
                displayTextBlock.Inlines.Add(run);
            }

            run = GenerateRun(displayText.Substring(position, searchstring.Length), true);

            if (run != null)
            {
                displayTextBlock.Inlines.Add(run);
            }

            compareText = compareText.Substring(position + searchstring.Length);
            displayText = displayText.Substring(position + searchstring.Length);
        }

        run = GenerateRun(displayText, false);

        if (run != null)
        {
            displayTextBlock.Inlines.Add(run);
        }

        base.OnRender(drawingContext);
    }

上述方法称之为:

    private Run GenerateRun(string searchedString, bool isHighlight)
    {
        if (!string.IsNullOrEmpty(searchedString))
        {
            Run run = new Run(searchedString)
            {
                Background = isHighlight ? this.HighlightBackground : this.Background,
                Foreground = isHighlight ? this.HighlightForeground : this.Foreground,

                // Set the source text with the style which is Italic.
                FontStyle = isHighlight ? FontStyles.Italic : FontStyles.Normal,

                // Set the source text with the style which is Bold.
                FontWeight = isHighlight ? FontWeights.Bold : FontWeights.Normal,
            };
            return run;
        }
        return null;
    }

我做了很多测试,但我找不到这个问题的来源。此外,我还为DataGridRow上的双击事件添加了处理程序:

enter image description here

这是正确的,最后一列的值为空,但是,如何在视图中填充它?

0 个答案:

没有答案