gpt4 book ai didi

c# - 从类中创建属性

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:26 24 4
gpt4 key购买 nike

我创建了一个类,它可以缩短文本以适合文本框,同时还添加“...”。

例子:

[ThisTextis]toolong > [ThisTex...]

我在 Windows 8.1 VM(虚拟机)上使用 Microsoft Visual Studio 2013,该软件是一个应用程序。

现在它可能并不完美,但效果非常好。我现在想知道的是,我是否可以从该类中创建一个 bool 属性,以便用户可以在 XAML 中的文本框上启用/禁用它:

<TextBox CutText="True"/>

问题是我已经在类中使用了预先存在的 Text 属性,我的代码没有我希望的那么漂亮。无论如何,我很乐意提供任何建议和/或帮助。

编辑:简单地说,我想为 TextBox 创建一个 TextBlock.TextTrimming 属性,因为已经存在的属性仅限于 TextBlock .

这是类:

 class CutText
{
//Create a new instance of a textbox
TextBox textCut = new TextBox();

//Reset at start
public void ResetText(TextBox text)
{
//Overwrite textCut with the chosen TextBox
textCut = text;
//Handles the possibility of already filled textbox
if (textCut.Text != "" || textCut.Text != null)
{
_text = textCut.Text;
}
//Prevents text from being 'Null'
else
{
_text = "";
}
}

//Cuts text to width of textbox
private string CutTextToWidth(string text, double fontSize, double width)
{
//boolean to check if width of text is correct
bool validArea = false;
//comply with difference in width of characters
double CharDiffLength = (stringWidth("M", fontSize) - stringWidth("|", fontSize));
//shortened text
string shortText = text;
//last length which was too long
int LastLongLen = text.Length;
//last length which fit into textbox
int LastFitLen = 0;

if (stringWidth(text, fontSize) < width)
{
shortText = text;
}
else
{
//repeat until the text fits into the appointed area
while (!validArea)
{
if (width < stringWidth(shortText, fontSize))
{
//text is still too long
LastLongLen = shortText.Length;
}
else
{
//text is not too long
LastFitLen = shortText.Length;
}

int newLen = (LastFitLen + LastLongLen) / 2;

if (shortText.Length != newLen)
{
//set shortened text
shortText = text.Substring(0, newLen) + "\u2026";
}
validArea = ((width - 10 < stringWidth(shortText, fontSize)) && (stringWidth(shortText, fontSize) < width));
}
}
//return the shortened text
return shortText;
}

//Calculate the width of the text
private double stringWidth(string s, double fontSize)
{
if (s == " ")
s = "\u00a0";

TextBlock t = new TextBlock()
{
FontSize = fontSize,
Text = s
};
t.Measure(new Size(double.MaxValue, double.MaxValue));
return t.ActualWidth;
}

//(GotFocus) Replaces cut text with full text and places the cursor at the chosen position
public void GotFocusText()
{
int index = textCut.SelectionStart;
textCut.Text = _text;
textCut.SelectionStart = index;
}

//(LostFocus) Saves cut text into property / empties the textbox if nothing has been written and sets tooltip
public void LostFocusText()
{
if (!string.IsNullOrWhiteSpace(textCut.Text))
{
Text = textCut.Text;
}
else
{
Text = "";
}
ToolTipService.SetToolTip(textCut, _text);
}

//TextBox.Text Property
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
//Receive text, fontsize and width of textbox
textCut.Text = CutTextToWidth(_text, textCut.FontSize, textCut.Width - 25);
}
}
}

最佳答案

很遗憾地通知您,您浪费了时间。 WPF 已经内置了该功能。您可以设置 TextBlock.TextTrimming propertyCharacterEllipsisWordEllipsis ,这将自动修剪控件的溢出文本并添加省略号 (...)。看这个简单的例子:

<TextBlock TextTrimming="CharacterEllipsis" Width="150">
Lorem ipsum dolor sit amet, consectetur adipisicing</TextBlock>

来自TextTrimming Enumeration MSDN 上的页面:

CharacterEllipsis: Text is trimmed at a character boundary. An ellipsis (...) is drawn in place of remaining text.

WordEllipsis: Text is trimmed at a word boundary. An ellipsis (...) is drawn in place of remaining text.


更新>>>

要更直接地回答您的问题,您应该在 Attached Property 中创建代码.这样,您可以定义一个 bool 附加属性来使用省略号,例如。像这样:

<TextBox TextBoxProperties.CutText="True" />

... 其中 TextBoxProperties 是定义附加属性的类的名称,CutText 是属性本身的名称。查看How to create an Attached Property section从链接页面找出如何做到这一点。您可以使用该属性中的 CutText 类。

关于c# - 从类中创建属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com