1.简单DataContext绑定
//绑定对象 label.DataContext = Process.GetCurrentProcess();
Xaml代码:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="24*"/> <ColumnDefinition Width="49*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="87"/> <RowDefinition/> </Grid.RowDefinitions> <Label x:Name="label" Margin="0,61,0,0" VerticalAlignment="Top" Height="26" Grid.Column="1"> <Label.Content> <Binding Path="ProcessName"></Binding> </Label.Content> </Label> <Label x:Name="label1" Content="当前进程名称:" Margin="-5,61,5,0" VerticalAlignment="Top" Height="26" /> </Grid>
显示结果:
2.使用MultiBinding,将多个值绑定同一属性
后台基本同上
Xaml代码:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="61*"/> <ColumnDefinition Width="185*"/> </Grid.ColumnDefinitions> <Label x:Name="label" Content="进程ID和进程名称:" Margin="0,117,0,0" VerticalAlignment="Top" Height="25"/> <TextBox x:Name="textBox" Grid.Column="1" Height="23" Margin="0,117,0,0" TextWrapping="Wrap" VerticalAlignment="Top" > <TextBox.Text> <MultiBinding StringFormat="{}{0}-->{1}"> <Binding Path="Id" Mode="OneWay"></Binding> <Binding Path="ProcessName" Mode="OneWay"></Binding> </MultiBinding> </TextBox.Text> </TextBox> </Grid>
显示结果: