It would be even better if you could just associate the two controls, without having to write any additional code. This is the goal of data binding.
Data binding is the association of two objects, such that one of the objects is always kept up-to-date with the value of the other. With data binding, you can just create an association between the TextBox.Text property and the Slider.Value property, and WPF will take care of the updates automatically as they occur. This association is called a binding.
对于两个控件之间的绑定有两种方式:
1、从XAML里就绑定
<StackPanel>
<TextBox Margin="10" Text="{Binding ElementName=sldrSlider, Path=Value}"/>
<Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/>
</StackPanel>
2、在后台代码中绑定
假设设计界面已经添加以下两个控件:
<StackPanel>
<Label Name="displayText"/>
<TextBox Name="sourceInfo"/>
</StackPanel>
后台绑定:
public Window1()
{
InitializeComponent();
Binding myBinding = new Binding();
myBinding.Source = sourceInfo;
myBinding.Path = new PropertyPath( "Text" );
// Connect the Source and the Target.
displayText.SetBinding( Label.ContentProperty, myBinding );
}
绑定有以下方向:
删除绑定:
<Window x:Class="ClearBindings.Window1" ...
Title="Clear Bindings" Height="152" Width="260">
<StackPanel>
<Label Name="displayText" Margin="5" FontSize="16"
Content="My Text"
FontFamily="{Binding ElementName=fontBox, Path=Text}"
FontWeight="{Binding ElementName=weightBox, Path=Text}"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel>
<ComboBox Name="fontBox" SelectedIndex="0" Margin="5,0,5,2">
<ComboBoxItem>Arial</ComboBoxItem>
<ComboBoxItem>Courier New</ComboBoxItem>
</ComboBox>
<ComboBox Name="weightBox" SelectedIndex="0" Margin="5,0,5,2">
<ComboBoxItem>Normal</ComboBoxItem>
<ComboBoxItem>Bold</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Grid.Column="1">
<Button Name="ClearFont" Margin="5,0,5,2"
Click="ClearFont_Click">Clear Font</Button>
<Button Name="ClearAll" Margin="5,0,5,2"
Click="ClearAll_Click">Clear All</Button>
<Button Name="CreateBindings" Margin="5,0,5,2"
Click="CreateBindings_Click">Create Bindings</Button>
</StackPanel>
</Grid>
</StackPanel>
</Window>
后台代码实现删除绑定、添加绑定:
public partial class Window1 : Window
{
public Window1() { InitializeComponent(); }
// Clear the FontFamily binding.
private void ClearFont_Click( object sender, RoutedEventArgs e )
{
BindingOperations.ClearBinding( displayText, FontFamilyProperty );
}
// Clear all the bindings.
private void ClearAll_Click( object sender, RoutedEventArgs e )
{
BindingOperations.ClearAllBindings( displayText );
}
// Re-create the two bindings.
private void CreateBindings_Click( object sender, RoutedEventArgs e )
{
// Create the FontFamily binding.
Binding fontBinding = new Binding();
fontBinding.Source = fontBox;
fontBinding.Path = new PropertyPath( "Text" );
fontBinding.Mode = BindingMode.OneWay;
displayText.SetBinding( FontFamilyProperty, fontBinding );
// Create the FontWeight binding.
Binding weightBinding = new Binding();
weightBinding.Source = weightBox;
weightBinding.Path = new PropertyPath( "Text" );
weightBinding.Mode = BindingMode.OneWay;
displayText.SetBinding( FontWeightProperty, weightBinding );
}
}
绑定到非控件:
So far, all the source items for which you’ve created bindings have been WPF elements that derive from UIElement. But the source object can be any type of object. In this section, I’ll show how to create a simple class and bind to several of its properties. Remember, however, that although the source object can be of any type, the Path must always point to a public property.
新建Person.cs文件:
class Person
{
public string FirstName { get; set; }
public int Age { get; set; }
public string FavoriteColor { get; set; }
public Person( string fName, int age, string color )
{
FirstName = fName;
Age = age;
FavoriteColor = color;
}
}
设计器中添加三个label:
<StackPanel Orientation="Horizontal">
<Label Name="lblFName" FontWeight="Bold"/>
<Label Name="lblAge"/>
<Label Name="lblColor"/>
</StackPanel>
后台代码实现绑定到Person类的三个属性:
public MainWindow()
{
InitializeComponent();
Person p = new Person("Shirley", 34, "Green");
Binding nameBinding = new Binding("FirstName");
nameBinding.Source = p;
lblFName.SetBinding(ContentProperty, nameBinding);
Binding ageBinding = new Binding("Age");
ageBinding.Source = p;
lblAge.SetBinding(ContentProperty, ageBinding);
Binding colorBinding = new Binding("FavoriteColor");
colorBinding.Source = p;
lblColor.SetBinding(ContentProperty, colorBinding);
}