两种形式绑定WPF

时间:2015-09-21 10:00:52

标签: wpf binding

我还在学习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>

1 个答案:

答案 0 :(得分:2)

我已根据您的需求创建了一个示例应用程序,并在此处上传:

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

我保持一切尽可能简单,因为你是WPF的新手,所以跳过了命令,MVVM等。

关于此上传的几点:

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

  2. 在您的XAML代码中,我更改了您显示播放器名称/分数的位置。喜欢: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> <!-- old c3p0 (0.9.1.1) in class path causes destruction of ComboPooledDataSource--> <exclusions> <exclusion> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> </exclusion> </exclusions> </dependency>

  3. 由于INotifyPropertyChanged,这种双向绑定魔法起作用。 我们的业务实体如下:

    <TextBox x:Name="txtNamaA" Text="{Binding Item1.Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="#FF8A9B0F"/>

    }

  4. 见下图:             Solution explanation