首页 > 解决方案 > 无法数据绑定 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; }
    }
}

标签: c#wpf

解决方案


您正在绑定到只读属性Gravity中的属性。ViewModel

您应该更改此代码:

public double Gravity => model.Gravity;

对此:

public double Gravity
{
    set
    {
        model.Gravity = value;
        GravityUpdated(this, new EventArgs());
    }
    get
    {
        return model.Gravity;
    }
}

推荐阅读