【问题标题】:Binding doesn't respond to PropertyChanged绑定不响应 PropertyChanged
【发布时间】:2013-08-04 11:16:58
【问题描述】:

我有一个窗口模型和一个视图模型,但我被视图卡住了。
该视图有一个包含以下内容的控件:

<TextBlock Text="{Binding Title}" [...] />

数据上下文是视图模型,它有一个字符串类型的Title属性。

问题是,即使值发生变化(我可以通过调试器看到它​​发生了变化)并且为 Title 调用了 PropertyChangedTextBlock 也不会变化(它保持为空)。

为了确认这一点,我实际上在每次按下鼠标时都调用了一个更新方法。

我听说有时TextBlocks 中的绑定有问题,所以我也尝试了一个标签,但没有成功。有什么建议么?如果有什么想看的代码,请在cmets中告诉我(整个东西挺大的)。

ViewModel
Code-behind for the View.

更清楚一点:Window 是我的模型,而不是 WPF 的窗口!

【问题讨论】:

  • 您确定没有绑定错误吗?你检查了Output 窗口吗?
  • @AnandMurali 是的,我总是检查它。
  • 能否出示一下Title Property的代码?
  • @NahumLitvin 我在底部添加了指向 ViewModel 的链接。 XAML 位于顶部(实际上就是全部)。
  • 不,不是。你可能搞砸了datacontext。(你的cs很好)

标签: c# wpf data-binding mvvm


【解决方案1】:

如果你实例化你的 WindowViewModel 类,它会起作用

代码隐藏:

public partial class WindowEntryView
{
    public static readonly DependencyProperty WindowViewModelProperty;
    public WindowViewModel WindowViewModel
    {
        get { return (WindowViewModel)GetValue(WindowViewModelProperty); }
        set
        {
            SetValue(WindowViewModelProperty, value);
            value.Refresh(); // I've putted this out of desperation to see if it would help.. it didn't.
        }
    }

    static WindowEntryView()
    {
        PropertyMetadata metadata = new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender); // Another attempt with no success.
        WindowViewModelProperty = DependencyProperty.Register("WindowViewModel", typeof(WindowViewModel), typeof(WindowEntryView), metadata);
    }

    public WindowEntryView()
    {
        //WindowViewModel = new WindowViewModel(19860522); This is the only attempt that made the label show something, but it didn't update later of course.

        InitializeComponent();

        WindowViewModel = new WpfApplication3.WindowViewModel() { Title = "Check" };
        DataContext = WindowViewModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowViewModel.Title = DateTime.Now.ToString();
    }
}

XAML:

<Grid>
    <TextBlock Text="{Binding Title}" VerticalAlignment="Top"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="116,131,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>

【讨论】:

  • 仔细查看源代码。他正试图以一种复杂的方式做到这一点。
  • @AnandMurali 我的 XAML 是为了什么?请具体一点。
  • 我已更新答案以包含我的示例 XAML。它对我来说很好,所以我认为你可能在 XAML 中犯了一个错误。
  • @AnandMurali 哦,我以前尝试过类似的东西。这将是第一次工作,但不会更新。
  • 它会更新,点击Button 并检查。 TextBlock 将更新为当前时间。
【解决方案2】:

你的 _window.Title 是 string.Empty 。赋予它一些价值。喜欢

public WindowViewModel()
{
    Window = new Window(){Title="abc"};
}

更新我尝试了你的代码,但它对我不起作用我做了一个更改并且它起作用了更改是

public WindowEntryView()
{
    DataContext = new WindowViewModel();
    InitializeComponent();
}

【讨论】:

  • 我不能。 Title 的设置器需要一个实际的窗口句柄才能运行。它更像是一种方法而不是变量(没有私有字段)。除了这有什么帮助?
  • 代码中WindowViewModel 没有实例化对吧?还是我错过了什么。
  • 是的,你是对的(我一直在玩这么多来修复它......)。但是即使我修复了它仍然没有更新。
  • @Ken 能否请您返回 get { return "abc"; } 而不是得到 { 返回 _window.Title; } 只是为了验证您的绑定是否正确
  • @ethicallogics 哦,我明白你的意思了。绑定有效,但只有一次。我知道这一点,因为如果没有句柄,模型会返回一个特定的字符串。问题正是标题所说的......它不响应任何 PropertyChanged 通知。所以我创建了视图,它显示了“无句柄”字符串。然后设置句柄,但还是说“无句柄”。
【解决方案3】:

您能否将属性名称(在您的情况下为“Title”)传递给 OnPropertyChanged 方法。

【讨论】:

  • 试过了,没用。虽然这并不奇怪,因为Refresh 有这个代码。
猜你喜欢
  • 2013-08-13
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多