我有三个texbox和一个滑块,用于更改其Text属性。我要做的是将slider的value属性与Text textbox属性绑定,但是以特定方式绑定。当其中一个文本框被激活(聚焦)我需要滑块来更改其Text属性。只有那一个。到目前为止我已经绑定了它,但是当我移动滑块时,所有文本框都会更新。
有什么想法吗? 我正在阅读有关转换器的内容,但我不知道如何在我的程序中实现它。
http://forums.create.msdn.com/forums/t/95548.aspx这里你有我的滑块和文本块的代码。
答案 0 :(得分:0)
当文本框获得焦点时,只需更改活动绑定即可:
代码背后:
private Binding _activeBinding;
private TextBox _activeTextbox;
private TextBox ActiveTextBox
{
get { return _activeTextbox; }
set
{
// Check if a binding exists, initialize if one does not
if (_activeBinding == null)
{
_activeBinding = new Binding("Value");
_activeBinding.Source = this.sld;
}
if (_activeTextbox != null)
{
// Clear the binding
_activeTextbox.ClearValue(TextBox.TextProperty);
}
_activeTextbox = value;
if (_activeTextbox != null)
{
// Set the new binding
_activeTextbox.SetBinding(TextBox.TextProperty, _activeBinding);
}
}
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.ActiveTextBox = sender as TextBox;
}
XAML:
<Grid>
<StackPanel>
<TextBox GotFocus="TextBox_GotFocus">1</TextBox>
<TextBox GotFocus="TextBox_GotFocus">2</TextBox>
<TextBox GotFocus="TextBox_GotFocus">3</TextBox>
<Slider x:Name="sld"></Slider>
</StackPanel>
</Grid>