【发布时间】:2015-12-17 23:36:07
【问题描述】:
我仍在学习 WPF,并尝试制作使用两种形式的简单评分程序。第一个窗体作为控制面板,第二个窗体作为显示。但是,当我在控制面板中更改值时,我找不到更改显示表单中值的方法。我用谷歌搜索了它,它说我可以使用绑定。你愿意帮助我吗?这是我的代码:
<Window x:Class="Score.Control"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Score"
mc:Ignorable="d"
Title="Control" Height="300" Width="400" MinHeight="300" MinWidth="400" Background="#FF181818" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0">
<TextBox x:Name="txtJudul1" Foreground="#FF00AADE">Judul 1</TextBox>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBox x:Name="txtJudul2" Foreground="#FFC6C6C6">Judul 2</TextBox>
</Viewbox>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0" Margin="2">
<TextBox x:Name="txtNamaA" Foreground="#FF8A9B0F">A</TextBox>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBox x:Name="txtScoreA" Foreground="#FFFCA910">1</TextBox>
</Viewbox>
<DockPanel HorizontalAlignment="Center" Grid.Row="2" Margin="5">
<Viewbox>
<Button x:Name="btnTambahA">+</Button>
</Viewbox>
<Viewbox>
<Button x:Name="btnKurangA">-</Button>
</Viewbox>
</DockPanel>
</Grid>
</DockPanel>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Viewbox>
<Button x:Name="btnDisplay" Grid.Row="0" Margin="0,5" Click="btnDisplay_Click">
DISPLAY</Button>
</Viewbox>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0">
<TextBlock Foreground="#FFC6C6C6"> VS </TextBlock>
</Viewbox>
<Grid Grid.Row="1" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="0">
<TextBox x:Name="txtPointA" Foreground="#FFF14D0F">x</TextBox>
</Viewbox>
<Viewbox Grid.Column="1">
<TextBlock Foreground="#FFC6C6C6">-</TextBlock>
</Viewbox>
<Viewbox Grid.Column="2">
<TextBox x:Name="txtPointB" Foreground="#FFF14D0F">y</TextBox>
</Viewbox>
</Grid>
</Grid>
<WrapPanel HorizontalAlignment="Center" Grid.Row="2" Margin="6">
<Viewbox>
<Button x:Name="btnReset" Height="20">R</Button>
</Viewbox>
<Viewbox>
<Button Name="btnSwitch">S</Button>
</Viewbox>
</WrapPanel>
</Grid>
<DockPanel Grid.Column="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0" Margin="2">
<TextBox x:Name="txtNamaB" Foreground="#FF8A9B0F">B</TextBox>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBox x:Name="txtScoreB" Foreground="#FFFCA910">0</TextBox>
</Viewbox>
<DockPanel HorizontalAlignment="Center" Grid.Row="2" Margin="5">
<Viewbox>
<Button x:Name="btnTambahB">+</Button>
</Viewbox>
<Viewbox>
<Button x:Name="btnKurangB">-</Button>
</Viewbox>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Grid>
<Window x:Class="Score.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Score"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="#FF181818"
WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0">
<TextBlock x:Name="txtJudul1" Foreground="#FF00AADE"> Judul 1 </TextBlock>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBlock Foreground="#FFC6C6C6"> Judul 2 </TextBlock>
</Viewbox>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0">
<TextBlock Foreground="#FF8A9B0F">A</TextBlock>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBlock Foreground="#FFFCA910">1</TextBlock>
</Viewbox>
</Grid>
</DockPanel>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0">
<TextBlock Foreground="#FFC6C6C6">VS</TextBlock>
</Viewbox>
<Grid Grid.Row="1" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="0">
<TextBlock Foreground="#FFF14D0F">x</TextBlock>
</Viewbox>
<Viewbox Grid.Column="1">
<TextBlock Foreground="#FFC6C6C6">-</TextBlock>
</Viewbox>
<Viewbox Grid.Column="2">
<TextBlock Foreground="#FFF14D0F">y</TextBlock>
</Viewbox>
</Grid>
</Grid>
</Grid>
<DockPanel Grid.Column="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Viewbox Grid.Row="0">
<TextBlock Foreground="#FF8A9B0F">B</TextBlock>
</Viewbox>
<Viewbox Grid.Row="1">
<TextBlock Foreground="#FFFCA910">0</TextBlock>
</Viewbox>
</Grid>
</DockPanel>
</Grid>
</Grid>
【问题讨论】:
-
从这里开始阅读:Data Binding Overview。
-
@Clemens :我只是不明白..如何通过在 Form 1 的文本框中设置其值来更改 Form2 中的 texblock?
-
通过将两个控件绑定到视图模型类的同一实例中的同一属性。同时在网上搜索 MVVM。