【问题标题】:TextBlock binding updates only once at the beginningTextBlock 绑定在开始时只更新一次
【发布时间】:2020-06-18 12:08:27
【问题描述】:

我的 .xaml 文件中有一个已绑定到属性的 texblock。还有 2 个按钮指向 ++ 和 -- 单击它们时该属性的值。问题是绑定仅在应用程序启动时起作用。

当按钮被点击时,相应的命令被执行并且变量在代码中改变了它的值(所以执行给定的方法没有问题)。

但是,在 UI 中看不到任何更改。我不确定这可能是什么原因,用户界面是否被某种方式阻止了?我一直使用 MVVM 方式工作,从来没有遇到过这样的绑定问题。

如果需要更多代码,请告诉我,我不想在这里放一堵巨大的代码墙。

构造函数:

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        Zoom = 60;

        ZoomPlusSceneCommand = new RelayCommand(ZoomPlus);
        ZoomMinusSceneCommand = new RelayCommand(ZoomMinus);
    }

处理属性已更改:

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

财产本身:

    private int zoom;
    public int Zoom
    {
        get { return zoom; }
        set { if (zoom != value) { zoom = value; RaisePropertyChanged("Zoom"); } }
    }

命令:

    public RelayCommand ZoomPlusSceneCommand { get; set; }
    public RelayCommand ZoomMinusSceneCommand { get; set; }

其中 RelayCommand 是我在大多数项目中用作模板的实现,并且它们一直运行良好(至少在 MVVM 项目中)

单击按钮时使用命令执行的方法:

    private void ZoomPlus(object o)
    {
        Zoom++;
    }
    private void ZoomMinus(object o)
    {
        Zoom--;
    }

Xaml:

<Window x:Class="Rendering3D.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:Rendering3D"
    Loaded="GetSceneSize"
    ResizeMode="NoResize"
    mc:Ignorable="d"
    Title="MainWindow" Height="550" Width="900">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition x:Name="SceneColumn" Width="*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="40"/>
        <ColumnDefinition Width="10"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="10"/>
        <RowDefinition x:Name="SceneRow" Height="*"/>
        <RowDefinition Height="10"/>
    </Grid.RowDefinitions>

    <Image x:Name="SceneImage" Grid.Column="1" Grid.Row="1" 
           PreviewMouseLeftButtonDown="SceneMouseLeftButtonDown" 
           MouseMove="SceneMouseMove"
           MouseWheel="SceneMouseWheel"/>

    <Grid Grid.Column="3" Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="Padding" Value="0 5 0 5"/>
            </Style>
        </Grid.Resources>

        <Button Grid.Row="0" Content="+" Command="{Binding ZoomPlusSceneCommand}"/>
        <Button Grid.Row="2" Content="-" Command="{Binding ZoomMinusSceneCommand}"/>
        <Label  Grid.Row="4" Content="Zoom" HorizontalAlignment="Center"/>
        <TextBlock Grid.Row="5" Text="{Binding Zoom}" HorizontalAlignment="Center"/>
    </Grid>
</Grid>

鼠标事件示例:

    private void SceneMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta > 0)
            Zoom++;
        else
            Zoom--;

        return;
    }

【问题讨论】:

  • 如何为绑定设置 DataContext?
  • @ASh 在 windows 构造函数中我有this.DataContext = this;
  • 您显示的内容看起来不错(尽管您可以improve it)。你能展示更多吗?
  • 尝试将Zoom++替换为Zoom += 1Zoom--替换为Zoom -= 1
  • @trix 没有帮助

标签: c# wpf data-binding


【解决方案1】:

假设属性已设置,如果您的 MainWindow 类实际上实现了 INotifyPropertyChanged,则应更新视图:

public partial class MainWindow : Window, INotifyPropertyChanged
...

【讨论】:

  • 缺少的东西。谢谢!
猜你喜欢
  • 2018-05-15
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 2014-09-07
相关资源
最近更新 更多