本文整理汇总了C#中System.Windows.Controls.TextBox.GetBindingExpression方法的典型用法代码示例。如果您正苦于以下问题:C# TextBox.GetBindingExpression方法的具体用法?C# TextBox.GetBindingExpression怎么用?C# TextBox.GetBindingExpression使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.TextBox
的用法示例。
在下文中一共展示了TextBox.GetBindingExpression方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TextBoxUpdateBinding
private static void TextBoxUpdateBinding(TextBox textBox)
{
// Source: http://stackoverflow.com/a/5631292/143684
var be = textBox.GetBindingExpression(TextBox.TextProperty);
if (be != null)
{
be.UpdateSource();
}
}
示例2: BuildTextBox
/// <summary>
/// Inialize necessary properties and hook necessary events on TextBox, then add it into tree.
/// </summary>
private void BuildTextBox()
{
if (_textBox == null)
{
_textBox = new TextBox();
_textBox.BorderThickness = new Thickness(1);
_textBox.BorderBrush = Brushes.Black;
_textBox.Background = Brushes.White;
_textBox.Padding = new Thickness(0);
_textBox.Margin = new Thickness(0);
_textBox.KeyDown +=
(KeyEventHandler)delegate(object sender, KeyEventArgs args)
{
if (_editBox.IsEditing && (args.Key == Key.Enter || args.Key == Key.Escape))
{
if (args.Key == Key.Escape)
{
BindingExpression bexp = _editBox.GetBindingExpression(EditBox.ActualValueProperty);
bexp.UpdateTarget();
}
_editBox.IsEditing = false;
_editBox._canBeEdit = false;
}
};
_textBox.GotKeyboardFocus +=
(KeyboardFocusChangedEventHandler)delegate(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox tb = (_textBox as TextBox);
tb.SelectionStart = tb.Text.Length;
};
_textBox.LostKeyboardFocus +=
(KeyboardFocusChangedEventHandler)delegate(object sender, KeyboardFocusChangedEventArgs e)
{
BindingExpression bexp = _textBox.GetBindingExpression(TextBox.TextProperty);
bexp.UpdateSource();
_editBox.IsEditing = false;
};
}
_canvas = new Canvas();
_canvas.Children.Add(_textBox);
_visualChildren.Add(_canvas);
//Bind Text onto AdornedElement.
Binding binding = new Binding("ActualValue");
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
binding.Source = _editBox;
_textBox.SetBinding(TextBox.TextProperty, binding);
//Binding binding = new Binding("Text");
//Update TextBox's focus status when layout finishs.
_textBox.LayoutUpdated += new EventHandler(
(EventHandler)delegate(object sender, EventArgs args)
{
if (_isVisible)
_textBox.Focus();
});
_textBox.Background = Brushes.Transparent;
_textBox.TextWrapping = TextWrapping.NoWrap;
ScrollViewer.SetCanContentScroll(_textBox, false);
ScrollViewer.SetVerticalScrollBarVisibility(_textBox, ScrollBarVisibility.Disabled);
ScrollViewer.SetHorizontalScrollBarVisibility(_textBox, ScrollBarVisibility.Disabled);
_textBox.GotFocus += delegate
{
_textBox.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
new ThreadStart(delegate
{
int pos = _textBox.Text.LastIndexOf(".");
if (pos == -1)
_textBox.SelectAll();
else _textBox.Select(0, pos);
}));
};
}
示例3: SetError
private void SetError(TextBox textBox, string error)
{
var textBoxBindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
if (textBoxBindingExpression == null) return;
ValidationError validationError =
new ValidationError(new RequiredValidationRule(),
textBoxBindingExpression) {ErrorContent = error};
Validation.MarkInvalid(
textBoxBindingExpression,
validationError);
}
示例4: SetText
void SetText(TextBox textBox, string value)
{
textBox.Text = value;
textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
示例5: GetRegexValidationRuleForTextBox
/// <summary>
/// Returns a RegexValidationRule to be used for validating the specified TextBox.
/// If the TextBox is not yet initialized, this method returns null.
/// </summary>
private static RegexValidationRule GetRegexValidationRuleForTextBox(TextBox textBox)
{
if (!textBox.IsInitialized)
{
// If the TextBox.Text property is bound, but the TextBox is not yet
// initialized, the property's binding can be null. In that situation,
// hook its Initialized event and verify the validation rule again when
// that event fires. At that point in time, the Text property's binding
// will be non-null.
EventHandler callback = null;
callback = delegate
{
textBox.Initialized -= callback;
VerifyRegexValidationRule(textBox);
};
textBox.Initialized += callback;
return null;
}
// Get the binding expression associated with the TextBox's Text property.
BindingExpression expression = textBox.GetBindingExpression(TextBox.TextProperty);
if (expression == null)
throw new InvalidOperationException(
"The TextBox's Text property must be bound for the RegexValidator to validate it.");
// Get the binding which owns the binding expression.
Binding binding = expression.ParentBinding;
if (binding == null)
throw new ApplicationException(
"Unexpected situation: the TextBox.Text binding expression has no parent binding.");
// Look for an existing instance of the RegexValidationRule class in the
// binding. If there is more than one instance in the ValidationRules
// then throw an exception because we don't know which one to modify.
RegexValidationRule regexRule = null;
foreach (ValidationRule rule in binding.ValidationRules)
{
if (rule is RegexValidationRule)
{
if (regexRule == null)
regexRule = rule as RegexValidationRule;
else
throw new InvalidOperationException(
"There should not be more than one RegexValidationRule in a Binding's ValidationRules.");
}
}
// If the TextBox.Text property's binding does not yet have an
// instance of RegexValidationRule in its ValidationRules,
// add an instance now and return it.
if (regexRule == null)
{
regexRule = new RegexValidationRule();
binding.ValidationRules.Add(regexRule);
}
return regexRule;
}
示例6: PrepareTextBox
private static void PrepareTextBox(TextBox textbox)
{
if (textbox == null) return;
var bindingExpression = textbox.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression == null) return;
var binding = bindingExpression.ParentBinding;
var newBinding = binding.Clone();
newBinding.ValidatesOnDataErrors = true;
textbox.SetBinding(TextBox.TextProperty, newBinding);
if (newBinding.UpdateSourceTrigger == UpdateSourceTrigger.PropertyChanged)
{
textbox.TextChanged += OnCoerceText;
}
else if (newBinding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus || newBinding.UpdateSourceTrigger == UpdateSourceTrigger.Default)
{
textbox.LostFocus += OnCoerceText;
}
}
示例7: MainWindow
public MainWindow()
{
InitializeComponent();
TextBox tb = new TextBox();
tb.GetBindingExpression(null);
}
示例8: UpdateText
/// <summary>
/// Updates the text in the text box, and updates the source of
/// the binding so that changes are reflected in the view model.
/// </summary>
private void UpdateText(TextBox textBox, string text)
{
BindingExpression bindingExpression;
textBox.Text = text;
bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
if ((object)bindingExpression != null)
bindingExpression.UpdateSource();
}
示例9: Validate
private bool Validate(TextBox tb)
{
BindingExpression expression = tb.GetBindingExpression(TextBox.TextProperty);
expression.UpdateSource();
return !expression.HasError;
}