晚上好,
我正在使用WPF应用程序,该应用程序使用数据绑定将类的变量绑定到UI。我已经设置了我的课程以合并 <Window x:Class="playerWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:playerWPF"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525"
Name="MainWindowName">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="112*"/>
<RowDefinition Height="207*"/>
</Grid.RowDefinitions>
<TextBox x:Name="answerBox" HorizontalAlignment="Left" Height="32" Margin="58,60,0,0" TextWrapping="Wrap" Text="Answer here..." VerticalAlignment="Top" Width="371" Grid.Row="1"/>
<Button x:Name="submitButton" Content="Submit" Click="submitButton_Click" HorizontalAlignment="Left" Height="35" Margin="199,97,0,0" VerticalAlignment="Top" Width="92" Grid.Row="1"/>
<Label x:Name="teamNameLabel" Content="{Binding TeamName}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="25" Width="74"/>
<Label x:Name="oppositionName" Content="{Binding OppositionName}" HorizontalAlignment="Left" Margin="328,9,0,0" VerticalAlignment="Top" Width="101"/>
<Label x:Name="responseLabel" Content="{Binding Question}" HorizontalAlignment="Left" Height="49" Margin="58,6,0,0" VerticalAlignment="Top" Width="371" Grid.Row="1"/>
<Label x:Name="teamScoreLabel" Content="{Binding TeamScore}" HorizontalAlignment="Left" Margin="19,35,0,0" VerticalAlignment="Top" Width="65"/>
<Label x:Name="oppositionScoreLabel" Content="{Binding OppositionScore}" HorizontalAlignment="Left" Margin="340,40,0,0" VerticalAlignment="Top" Width="65"/>
</Grid>
</Window>
,这在我的其他项目中运作良好。它目前看起来像这样:
MainWindow
我的XAML看起来像这样:
private static GameInfo gameInfo;
我将此类的类型初始化为gameInfo = new GameInfo("Team");
中的静态字段,如下所示:if(serverVersion == null)
在主窗口构造函数中,我为其分配了一个新实例:#include <iostream>
using namespace std;
int main() {
std::string string = "9 9";
std::cout << string;
return 0;
}
我的理解是,这应该将UI上的一些标签更改为其默认构造值,但事实并非如此。任何人都可以放弃任何光明吗?
答案 0 :(得分:2)
如果您遵循MVVM模式,则希望像使用View Model一样使用DataContext = gameInfo;
。所以,改变
{Binding <property name>}
到
{{1}}
然后您的绑定只是
{{1}}