gpt4 book ai didi

c# - 绑定(bind)到自定义用户控件的 DependencyProperty 不更新更改

转载 作者:可可西里 更新时间:2023-11-01 08:21:24 24 4
gpt4 key购买 nike

我在自定义用户控件上进行数据绑定(bind)时遇到困难。我创建了一个示例项目来突出我的问题。我是 WPF 的新手,本质上也是 MVVM,所以请多多包涵...

我创建了一个使用数据绑定(bind)两种方式的简单 View 。内置控件上的数据绑定(bind)工作得很好。我的自定义控件没有...我在控件的 PropertyChangedCallback 中放置了一个断点。它在启动时被击中一次,但之后再也不会。同时,我绑定(bind)到相同值的标签正在快乐地倒计时。

我错过了什么?我的示例项目如下:

主窗口:

<Window x:Class="WpfMVVMApp.MainWindow"
xmlns:local="clr-namespace:WpfMVVMApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.DataContext>
<local:CountdownViewModel />
</Grid.DataContext>
<Label Name="custName" Content="{Binding Path=Countdown.ChargeTimeRemaining_Mins}" Height="45" VerticalAlignment="Top"></Label>
<local:UserControl1 MinutesRemaining="{Binding Path=Countdown.ChargeTimeRemaining_Mins}" Height="45"></local:UserControl1>
</Grid>
</Window>

这是我的模型:

namespace WpfMVVMApp
{

public class CountdownModel : INotifyPropertyChanged
{
private int chargeTimeRemaining_Mins;
public int ChargeTimeRemaining_Mins
{
get
{
return chargeTimeRemaining_Mins;
}
set
{
chargeTimeRemaining_Mins = value;
OnPropertyChanged("ChargeTimeRemaining_Mins");
}
}

#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}

View 模型:

namespace WpfMVVMApp
{
public class CountdownViewModel
{
public CountdownModel Countdown { get; set; }

DispatcherTimer timer;
private const int maxMins = 360;

public CountdownViewModel()
{
Countdown = new CountdownModel { ChargeTimeRemaining_Mins = 60 };

// Setup timers
timer = new DispatcherTimer();
timer.Tick += new EventHandler(this.SystemChargeTimerService);
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}

private void SystemChargeTimerService(object sender, EventArgs e)
{
//convert to minutes remaining
// DEMO CODE - TODO: Remove
this.Countdown.ChargeTimeRemaining_Mins -= 1;
}
}
}

这是我的用户控件的 XAML:

<UserControl x:Class="WpfMVVMApp.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Name="Readout"></Label>
</Grid>
</UserControl>

下面是用户控件背后的代码:

namespace WpfMVVMApp
{
public partial class UserControl1 : UserControl
{
#region Dependency Properties
public static readonly DependencyProperty MinutesRemainingProperty =
DependencyProperty.Register
(
"MinutesRemaining", typeof(int), typeof(UserControl1),
new UIPropertyMetadata(10, new PropertyChangedCallback(minutesRemainChangedCallBack))
);
#endregion

public int MinutesRemaining
{
get
{
return (int)GetValue(MinutesRemainingProperty);
}
set
{
SetValue(MinutesRemainingProperty, value);
}
}

static void minutesRemainChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
UserControl1 _readout = (UserControl1)property;
_readout.MinutesRemaining = (int)args.NewValue;

_readout.Readout.Content = _readout.MinutesRemaining;
}

public UserControl1()
{
InitializeComponent();
}
}
}

最佳答案

您的更改回调正在破坏绑定(bind)。

作为骨架:在您的窗口中您有 UC.X="{Binding A}" 然后在该属性更改(在 UC 中)中您有 X=B;。这会破坏绑定(bind),因为在这两种情况下您都设置了 X

要纠正,请删除更改回调并将其添加到标签中:

 Content="{Binding MinutesRemaining, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"

关于c# - 绑定(bind)到自定义用户控件的 DependencyProperty 不更新更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com