无法数据绑定滑块 WPF 的值 [重复]
Posted
技术标签:
【中文标题】无法数据绑定滑块 WPF 的值 [重复]【英文标题】:Cannot data bind Value of a Slider WPF [duplicate] 【发布时间】:2020-08-02 04:59:33 【问题描述】:我正在努力将滑块的值绑定到使用 MVVM 的模型中的值。返回错误“TwoWay 或 OneWayToSource 绑定无法在‘SliderTest.ViewModel’类型的只读属性‘Gravity’上工作。”尽管有问题的属性在模型中一直是公开的。
我已经在一个简单的测试环境中复制了这个问题
MainWindow.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>
MainWindow.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();
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;
Item.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;
【问题讨论】:
请注意,错误消息并没有抱怨属性(在视图模型中)是非公开的,而是关于它是只读的。 【参考方案1】:您正在绑定到ViewModel
中的属性Gravity
,这是一个只读属性。
您应该更改此代码:
public double Gravity => model.Gravity;
到这个:
public double Gravity
set
model.Gravity = value;
GravityUpdated(this, new EventArgs());
get
return model.Gravity;
【讨论】:
感谢您的回答。我曾认为“=>”运算符有效地将视图模型中的属性映射到模型中的属性,这是两种方式。我是否错过了 lambda 运算符如何在这里工作的一个方面?以上是关于无法数据绑定滑块 WPF 的值 [重复]的主要内容,如果未能解决你的问题,请参考以下文章