WPF RibbonTextBox宽度问题

发布于 2024-08-27 03:15:23 字数 697 浏览 8 评论 0原文

在我看来,功能区控件的文本框有问题。我期待常见的 TextBox 控件行为:固定宽度和文本超出宽度时可见的插入符号。但是 RibbonTextBox 控件会更改其宽度,并且当文本超出右限时,溢出是不可见的。

我在博客上发现了一个黑客可以做这样的事情:

var img = SearchButton.Template.FindName("image", SearchButton);
if (img != null && img is Image)
   (img as Image).Visibility = Visibility.Collapsed;
var lbl = FindTemplateControl<Label>(SearchText);

var border = SearchText.Template.FindName("Bd", SearchText);

if (border != null && border is Border && img != null && lbl != null)
{
    (border as Border).Width = SearchText.ActualWidth - (((Image)img).ActualWidth + lbl.ActualWidth);
}

但我真的不想做这样的解决方法。还有其他更简单的方法来实现简单的 TextBox 行为吗?

It seems to me that the Ribbon control has a problem with textboxes. I was expecting a common TextBox control behaviour: a fixed width and a visible caret when the text exceeds the width. But the RibbonTextBox control changes its width and when the text exceeds the right limit, the overflow is not visible.

I found a hack on a blog that does something like this:

var img = SearchButton.Template.FindName("image", SearchButton);
if (img != null && img is Image)
   (img as Image).Visibility = Visibility.Collapsed;
var lbl = FindTemplateControl<Label>(SearchText);

var border = SearchText.Template.FindName("Bd", SearchText);

if (border != null && border is Border && img != null && lbl != null)
{
    (border as Border).Width = SearchText.ActualWidth - (((Image)img).ActualWidth + lbl.ActualWidth);
}

but I reallly don't want to do such a workaround. Is there any other simpler way to achieve simple TextBox behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠栀 2024-09-03 03:15:23

有一个属性“TextBoxWidth”未显示在设计器中,但也可以在 XAML 中使用:

<ribbon:RibbonTextBox Label="abc" TextBoxWidth="300" />

There is a Property "TextBoxWidth" which is not shown in the designer but can be used also in XAML:

<ribbon:RibbonTextBox Label="abc" TextBoxWidth="300" />
无敌元气妹 2024-09-03 03:15:23

显然,RibbonTextBox 不是一个简单的 TextBox;它是一个文本框。它实际上是一个堆栈面板,其中包含:
图像+标签+边框。实际上,它的模板有这样的内容:

<RibbonTextBox>
    <StackPanel>
        <Image/>
        <Label>
            <Border>
                <ContentPresenter>
                    <TextBlock/>
                </ContentPresenter>
            </Border>
        </Label>
        <Border>
            <ScrollViewer>
                <Grid>
                    <Rectangle>
                    </Rectangle>
                    <ScrollContentPresenter>
                        <TextBoxView>
                            <DrawingVisual/>
                        </TextBoxView>
                        <AdornerLayer/>
                    </ScrollContentPresenter>
                    <ScrollBar/>
                    <ScrollBar/>
                </Grid>
            </ScrollViewer>
        </Border>
    </StackPanel>
</RibbonTextBox>

所以,当你设置 RibbonTextBox 的宽度时,你实际上并不是设置文本框的宽度,而是设置整个控件的宽度。

我的建议是创建一个派生自 RibbonTextBox 的类,并在该类中实现 Loaded 事件处理程序,就像您在帖子中给出的示例一样。但请记住,图像、标签和边框具有额外的边距和填充,这将为您在文本框的左侧提供额外的空间。

Apparently, the RibbonTextBox isn't a simple TextBox; it's actually a stackpanel with:
image + label + border. Actually, its template has this content:

<RibbonTextBox>
    <StackPanel>
        <Image/>
        <Label>
            <Border>
                <ContentPresenter>
                    <TextBlock/>
                </ContentPresenter>
            </Border>
        </Label>
        <Border>
            <ScrollViewer>
                <Grid>
                    <Rectangle>
                    </Rectangle>
                    <ScrollContentPresenter>
                        <TextBoxView>
                            <DrawingVisual/>
                        </TextBoxView>
                        <AdornerLayer/>
                    </ScrollContentPresenter>
                    <ScrollBar/>
                    <ScrollBar/>
                </Grid>
            </ScrollViewer>
        </Border>
    </StackPanel>
</RibbonTextBox>

So, when you set the width of a RibbonTextBox, you don't actually set the width of the textbox, but the entire control's width.

My suggestion would be to create a class that derives from RibbonTextBox and implement in this class the Loaded event handler just like the example you gave in your post. But keep in mind that the image, label and border have additional margin and padding which will give you extra space on the left of the textbox.

嗼ふ静 2024-09-03 03:15:23

您可以这样设置宽度:

var textBox = new RibbonTextBox() { Label = "Label", Text = "Text", TextBoxWidth = 150 };

You can set width like this:

var textBox = new RibbonTextBox() { Label = "Label", Text = "Text", TextBoxWidth = 150 };

十秒萌定你 2024-09-03 03:15:23

RibbonTextBox 的问题是,当您按“enter”键时,不会触发 KeyDown 和 KeyUp 事件。因此,当您按“enter”键时,不会触发 LostFocus() 事件。

The problem with RibbonTextBox is that the KeyDown and KeyUp event are not triggered when you press "enter" key. As an result, the LostFocus() event is not triggered when you press the "enter" key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文