两种形式绑定 WPF
Two Forms Binding WPF
我仍在学习 WPF,我尝试制作使用两种形式的简单评分程序。第一个作为控制面板,第二个作为显示。但是,当我更改控制面板中的值时,我找不到更改显示表单中的值的方法。我用谷歌搜索了一下,它说我可以使用 Binding。你愿意帮助我吗?这是我的代码:
<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>
我已经根据您的需要创建了一个示例应用程序并上传到这里:
https://www.dropbox.com/s/4fiia3wpu8pnrkf/General.rar?dl=0
由于您是 WPF 的新手,我尽可能地简化了一切,因此跳过了命令、MVVM 等。
关于这次上传的几点:
您可以毫无问题地提取和 运行。
在您的 XAML 代码中,我对您显示播放器 names/scores 的位置进行了更改。喜欢:<TextBox x:Name="txtNamaA" Text="{Binding Item1.Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="#FF8A9B0F"/>
.
由于 INotifyPropertyChanged,这种 2 向绑定魔法起作用了。
我们的业务实体如下所示:
using System;
using System.ComponentModel;
using System.Collections.Generic;
namespace General.Score
{
public class ControlPanel
{
static Tuple<PlayerScore, PlayerScore> _playerPairs;
public static Tuple<PlayerScore, PlayerScore> playerPairs { get { return _playerPairs; } }
static ControlPanel()
{
PlayerScore p1 = new PlayerScore() { Name = "A", Score = 0 };
PlayerScore p2 = new PlayerScore() { Name = "B", Score = 0 };
_playerPairs = new Tuple<PlayerScore, PlayerScore>(p1, p2);
}
}
public class PlayerScore : INotifyPropertyChanged
{
String _name;
public String Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } }
int _score;
public int Score { get { return _score; } set { _score = value; OnPropertyChanged("Score"); } }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
}
见下图:
我仍在学习 WPF,我尝试制作使用两种形式的简单评分程序。第一个作为控制面板,第二个作为显示。但是,当我更改控制面板中的值时,我找不到更改显示表单中的值的方法。我用谷歌搜索了一下,它说我可以使用 Binding。你愿意帮助我吗?这是我的代码:
<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>
我已经根据您的需要创建了一个示例应用程序并上传到这里:
https://www.dropbox.com/s/4fiia3wpu8pnrkf/General.rar?dl=0
由于您是 WPF 的新手,我尽可能地简化了一切,因此跳过了命令、MVVM 等。
关于这次上传的几点:
您可以毫无问题地提取和 运行。
在您的 XAML 代码中,我对您显示播放器 names/scores 的位置进行了更改。喜欢:
<TextBox x:Name="txtNamaA" Text="{Binding Item1.Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="#FF8A9B0F"/>
.由于 INotifyPropertyChanged,这种 2 向绑定魔法起作用了。 我们的业务实体如下所示:
using System; using System.ComponentModel; using System.Collections.Generic; namespace General.Score { public class ControlPanel { static Tuple<PlayerScore, PlayerScore> _playerPairs; public static Tuple<PlayerScore, PlayerScore> playerPairs { get { return _playerPairs; } } static ControlPanel() { PlayerScore p1 = new PlayerScore() { Name = "A", Score = 0 }; PlayerScore p2 = new PlayerScore() { Name = "B", Score = 0 }; _playerPairs = new Tuple<PlayerScore, PlayerScore>(p1, p2); } } public class PlayerScore : INotifyPropertyChanged { String _name; public String Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } int _score; public int Score { get { return _score; } set { _score = value; OnPropertyChanged("Score"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string p) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(p)); } }
}
见下图: