3

選択したフィールドの上下にAdornerを追加して、このWPFDateTimePickerコントロールを強化したいと思います。

DateTimePicker

そのためには、TextBoxで選択範囲の最初と最後のX座標を把握する必要があります。どうやってやるの?

4

1 に答える 1

2

(編集:Derp-私はあなたの「選択のみ」の部分を逃しました...これはあなたに全文の長さを与えるでしょう)

探しているプロパティExtentWidthはTextBoxにあります。

(申し訳ありませんが、LINQPadしか手元にないので、これを難しい方法で行う必要があります...)

var wnd = new Window();
var panel = new StackPanel();
wnd.Content = panel;

var textBox = new TextBox();
textBox.Name = "theTextBox";
textBox.FontFamily = new FontFamily("Arial");
textBox.FontSize = 20;
textBox.Text = "This is some text I want to measure";
panel.Children.Add(textBox);

wnd.Show();

var showWidth = new TextBlock();
showWidth.Text = "This should show the size of the text";
var binding = new System.Windows.Data.Binding();
binding.Path = new PropertyPath("ExtentWidth");
binding.Source = textBox;
binding.Mode = System.Windows.Data.BindingMode.OneWay;
showWidth.SetBinding(TextBlock.TextProperty, binding);
panel.Children.Add(showWidth);

編集#2:わかりました、代わりにこれを試してください:(繰り返しますが、LINQPadのみ)

void Main()
{
    var wnd = new Window();
    var panel = new StackPanel();
    var textBox = new TextBox();
    textBox.FontSize = 20;
    textBox.Text = "This is some text I want to measure";
    textBox.SelectionChanged += OnSelectionChanged;
    showWidth = new TextBlock();
    panel.Children.Add(textBox);
    panel.Children.Add(showWidth);
    wnd.Content = panel;
    wnd.Show();
}

private TextBlock showWidth;

private void OnSelectionChanged(object sender, EventArgs args)
{
    var tb = sender as TextBox;
    double left = tb.GetRectFromCharacterIndex(tb.SelectionStart).Left;
    showWidth.Text = string.Format("{0:0}px", left);
    showWidth.Margin = new Thickness(left, 0, 0, 0);
}
于 2012-11-14T19:33:27.793 に答える