c# - 无法数据绑定 Slider WPF 的值
问题描述
我正在努力将滑块的值绑定到使用 MVVM 的模型中的值。返回错误“TwoWay 或 OneWayToSource 绑定无法在‘SliderTest.ViewModel’类型的只读属性‘Gravity’上工作。” 尽管有问题的财产在模型中一直是公开的。
我已经在一个简单的测试环境中复制了这个问题
主窗口.xaml
<Window x:Class="SliderTest.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:SliderTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Slider x:Name="GravitySlider" Height="25" Panel.ZIndex="-1" SmallChange="0" IsSnapToTickEnabled="True" Value="{Binding Gravity, Mode=TwoWay}" />
</Grid>
</Window>
主窗口.xaml.cs
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.Navigation;
using System.Windows.Shapes;
namespace SliderTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
}
视图模型.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SliderTest
{
class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private void GravityUpdated(object sender, EventArgs e)
{
OnPropertyChanged("Gravity");
}
private Model model;
public ViewModel()
{
model = new Model();
model.GravityUpdated += GravityUpdated;
}
public double Gravity => model.Gravity;
}
}
模型.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SliderTest
{
public class Model
{
public Model()
{
Item.Gravity = 1;
}
public double Gravity
{
set
{
Item.Gravity = value;
GravityUpdated(this, new EventArgs());
}
get
{
return Item.Gravity;
}
}
public event EventHandler GravityUpdated;
}
}
物品.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SliderTest
{
class Item
{
private static double gravity;
public static double Gravity { get => gravity; set => gravity = value; }
}
}
解决方案
您正在绑定到只读属性Gravity
中的属性。ViewModel
您应该更改此代码:
public double Gravity => model.Gravity;
对此:
public double Gravity
{
set
{
model.Gravity = value;
GravityUpdated(this, new EventArgs());
}
get
{
return model.Gravity;
}
}
推荐阅读
- javascript - 如何将 localStorage 添加到我的多语言网站
- python - 使分配给类变量的对象知道父级
- c - 任何人都可以告诉我为什么这个结构的大小在 32 位架构上是 16?
- android - Android 模拟器在“adb disable-verity”或“adb remount”后卡在重启
- javascript - 转换为包含在数组中的小写特定单词,只要它们出现在字符串中
- javascript - 重组对象数组
- ios - 部署到 TestFlight 后,Apple 登录无法正常工作
- python - 给定合同的 Python 问题(没有 if 语句或循环)
- ruby - 在哈希中查找最高/最大值
- excel - VBA Excel:评估数据验证规则