【问题标题】:Two Forms Binding WPF两种表单绑定 WPF
【发布时间】: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。

标签: wpf binding


【解决方案1】:

我根据您的需要创建了一个示例应用程序并上传到这里:

https://www.dropbox.com/s/4fiia3wpu8pnrkf/General.rar?dl=0

因为您是 WPF 的新手,所以我让一切尽可能简单,因此跳过了命令、MVVM 等。

关于此上传的几点:

  1. 您可以毫无问题地提取和运行。

  2. 在您的 XAML 代码中,我对您显示玩家名称/分数的位置进行了更改。喜欢:&lt;TextBox x:Name="txtNamaA" Text="{Binding Item1.Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="#FF8A9B0F"/&gt;

  3. 由于 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));
                    }
                }
    

    }

见下图:

【讨论】:

  • 如果它符合您的需要,也请将此标记为答案。
  • 非常感谢,但我仍然很困惑 Item1 是什么? &lt;TextBox x:Name="txtNamaA" Text="{Binding Item1.Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="#FF8A9B0F"/&gt;
  • 元组 bestOfThree = new Tuple();现在输入 bestOfThree。智能感知将显示 Item1、Item2、Item3,因为我们的元组包含 3 个项目。
猜你喜欢
  • 2017-01-03
  • 2012-07-31
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多