C# MVVM灯光绑定路径不工作
我试图绑定一个变量以显示在标签的内容中。 我使用Galasoft的MVVM light来实现这一点。 我发送我的C# MVVM灯光绑定路径不工作,c#,wpf,mvvm,C#,Wpf,Mvvm,我试图绑定一个变量以显示在标签的内容中。 我使用Galasoft的MVVM light来实现这一点。 我发送我的视图模型、我的窗口和我的窗口.cs。问题是,当我增加值时,不会发生任何事情,但应该会发生一些事情 窗口:我只绑定了一个标签 <Window x:Class="DPCKOU_prog3hf_pong.Settings" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=
视图模型
、我的窗口
和我的窗口.cs
。问题是,当我增加值时,不会发生任何事情,但应该会发生一些事情
窗口:我只绑定了一个标签
<Window x:Class="DPCKOU_prog3hf_pong.Settings"
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:DPCKOU_prog3hf_pong"
mc:Ignorable="d"
Title="Settings" Height="406" Width="717"
Background="{StaticResource MainMenuBG}"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
>
<Grid>
<StackPanel>
<Label x:Name="Title" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch"
Foreground="#00e4ff" FontSize ="28" Content="Size of pad"
Margin="10,30,10,10">
<Label.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Label.BitmapEffect>
</Label>
<Label x:Name="Size" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch"
Foreground="#00e4ff" FontSize ="40" Content="{Binding Path=PadSize}"
Margin="10,0,10,10">
<Label.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Label.BitmapEffect>
</Label>
<DockPanel>
<Button x:Name="Increase" Content="Increase" HorizontalAlignment="Left"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="120,10,10,10"
FontSize="18" BorderThickness="0" Click="Increase_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
<Button x:Name="Decrease" Content="Decrease" HorizontalAlignment="Right"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,120,10"
FontSize="18" BorderThickness="0" Click="Decrease_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
</DockPanel>
<Button x:Name="Play" Content="Play" HorizontalAlignment="Center"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,10,10"
FontSize="18" BorderThickness="0" Click="Play_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
<Button x:Name="Back" Content="Back" HorizontalAlignment="Center"
Width="149" Height="46" Foreground="#00e4ff" Background="Black" Margin="10,10,10,10"
FontSize="18" BorderThickness="0" Click="Back_Click">
<Button.BitmapEffect>
<DropShadowBitmapEffect Color="#00e4ff" ShadowDepth="0" Opacity="1"/>
</Button.BitmapEffect>
</Button>
</StackPanel>
</Grid>
和窗口.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
namespace DPCKOU_prog3hf_pong
{
class SettingsVM : ViewModelBase
{
int padSize;
public int PadSize
{
get
{
return padSize;
}
set
{
if(value >=1 && value <= 6)
{
Set(ref padSize, value);
}
}
}
public SettingsVM()
{
padSize = 1;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
namespace DPCKOU_prog3hf_pong
{
/// <summary>
/// Interaction logic for SingleSettings.xaml
/// </summary>
///
/*
* take it 100 pixels is the 4 units of the pad's size respectively.
* so 25 shall be 1 and that is the default value one can input.
* the player can input pad size from 1 to 6 meaning from 25 pixels to 150.
*/
public partial class Settings : Window
{
/*
* its sole purpose is to change the pad size.
*/
SettingsVM svm;
bool isPlaymodeSingle;
public Settings(bool isPlaymodeSingle)
{
InitializeComponent();
this.isPlaymodeSingle = isPlaymodeSingle;
svm = new SettingsVM();
}
private void Play_Click(object sender, RoutedEventArgs e)
{
if (isPlaymodeSingle)
{
//yes, solo
SinglePlayer spgame = new SinglePlayer();
spgame.Show();
Close();
/*
* show the game window and close this.
*/
}
else
{
//nope, multi.
}
}
private void Decrease_Click(object sender, RoutedEventArgs e)
{
svm.PadSize--;
}
private void Increase_Click(object sender, RoutedEventArgs e)
{
svm.PadSize++;
}
private void Back_Click(object sender, RoutedEventArgs e)
{
MainWindow mw = new MainWindow();
mw.Show();
Close();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用GalaSoft.MvvmLight;
命名空间DPCKOU_prog3hf_pong
{
///
///SingleSettings.xaml的交互逻辑
///
///
/*
*假设100像素分别是焊盘大小的4个单位。
*所以25应该是1,这是可以输入的默认值。
*播放器可以输入1到6的键盘大小,即25到150像素。
*/
公共部分类设置:窗口
{
/*
*其唯一目的是改变焊盘尺寸。
*/
设置支持向量机;
bool-isPlaymodeSingle;
公共设置(bool isPlaymodeSingle)
{
初始化组件();
this.isPlaymodeSingle=isPlaymodeSingle;
svm=新设置svm();
}
私有无效播放\单击(对象发送者,路由目标e)
{
如果(isPlaymodeSingle)
{
//是的,索洛
单人游戏spgame=新单人游戏();
spgame.Show();
Close();
/*
*显示游戏窗口并关闭此窗口。
*/
}
其他的
{
//不,是多重的。
}
}
私有无效减少\u单击(对象发送者,路由目标e)
{
svm.PadSize--;
}
私有无效增加\单击(对象发送者,路由目标e)
{
PadSize++;
}
私有无效返回\u单击(对象发送者,路由目标e)
{
主窗口mw=新的主窗口();
mw.Show();
Close();
}
}
}
您忘记设置窗口的位置
例如,您可以在Settings类的构造函数中执行此操作:
public Settings(bool isPlaymodeSingle)
{
InitializeComponent();
this.isPlaymodeSingle = isPlaymodeSingle;
svm = new SettingsVM();
DataContext = svm; // here
}
你忘了设置窗户的高度
例如,您可以在Settings类的构造函数中执行此操作:
public Settings(bool isPlaymodeSingle)
{
InitializeComponent();
this.isPlaymodeSingle = isPlaymodeSingle;
svm = new SettingsVM();
DataContext = svm; // here
}
显然,您从未设置窗口的DataContext。添加
DataContext=svm代码>到设置构造函数。你不是当时的英雄吗:它成功了。您可能希望将其转换为答案,以便我可以接受。显然,您从未设置窗口的DataContext。添加DataContext=svm代码>到设置构造函数。你不是当时的英雄吗:它成功了。你可能想把它转换成一个答案,这样我就可以接受了。