C# 自定义控件从视图模型更新依赖项属性

C# 自定义控件从视图模型更新依赖项属性,c#,wpf,custom-controls,dependency-properties,C#,Wpf,Custom Controls,Dependency Properties,我的自定义控件有问题。当我调用notify属性changed时,我的属性的getter永远不会被调用。我到处寻找,我不认为我打破了我的束缚(虽然我可能是错的)。 我的控制如下: XAML <UserControl x:Class="ClassNamespace.ClassName" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://sche

我的自定义控件有问题。当我调用notify属性changed时,我的属性的getter永远不会被调用。我到处寻找,我不认为我打破了我的束缚(虽然我可能是错的)。 我的控制如下:

XAML

<UserControl x:Class="ClassNamespace.ClassName"
         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" 
         xmlns:mui="http://firstfloorsoftware.com/ModernUI"
         xmlns:default="clr-namespace:PullTabs.Applications.FrontEnd.Views.Default"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         >

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Text="Sample Text" />
    <DataGrid Grid.Row="1" FontSize="20" ItemsSource="{Binding Bills, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type default:ClassName}}}" />
    <TextBlock Text="Sample Text" />
</Grid>

当我显式调用命名PeriodTransactions属性的property changed方法时,我希望PeriodTransactions属性的getter随后会执行。但是,在getter中设置了断点后,仅在创建视图时调用断点,以后不再调用断点。请提供一些见解,了解为什么自定义控件中的依赖项属性没有更新,或者为什么自定义控件的依赖项属性没有调用getter。

是否在初始绑定上调用了UpdateBillCounts方法?是的。这一切都适用于初始绑定
GetMyDataFromDB
是否总是返回相同的集合实例?您的代码只有在每次调用时返回一个新的
IEnumerable
时才能工作。如果我的getter中的断点被击中,我会同意。另外,我知道它不会返回相同的数据,因为如果我离开并再次进入视图,则在命中getter时,自定义控件具有正确的数据。
public partial class ClassName
{
    public static DependencyProperty TransactionsProperty = DependencyProperty.Register("Transactions",
        typeof (IEnumerable<Transaction>), typeof (BillCounts),
        new UIPropertyMetadata(new List<Transaction>(), OnTransactionsPropertyPropertyChangedCallback));

    private static void OnTransactionsPropertyPropertyChangedCallback(DependencyObject source,
        DependencyPropertyChangedEventArgs e)
    {
        UpdateBillCounts((IEnumerable<Transaction>) e.NewValue);
    }

    public static ObservableCollection<Bill> Bills { get; } = new ObservableCollection<Bill>();

    public IEnumerable<Transaction> Transactions
    {
        get { return (IEnumerable<Transaction>) GetValue(TransactionsProperty); }
        set { SetValue(TransactionsProperty, value); }
    }

    public BillCounts()
    {
        InitializeComponent();
    }

    private static void UpdateBillCounts(IEnumerable<Transaction> transactions)
    {
        var lifetimeBillsAccepted = new Dictionary<decimal, int>();
        Bills.Clear();
        foreach (var bill in transactions)
        {
            if (lifetimeBillsAccepted.ContainsKey(bill.Amount))
            {
                lifetimeBillsAccepted[bill.Amount] = lifetimeBillsAccepted[bill.Amount] + 1;
            }
            else
            {
                lifetimeBillsAccepted.Add(bill.Amount, 1);
            }
        }
        foreach (var bill in lifetimeBillsAccepted)
        {
            Bills.Add(new Bill($"{bill.Key:C2}", bill.Value));
        }

        Bills.Add(new Bill("Total", Bills.Sum(bill => bill.Count)));

    }
}
<Default:BillCounts Grid.Column="0" Margin="0,10" Transactions="{Binding PeriodTransactions}" />
public IEnumerable<Transaction> PeriodTransactions
    {
        get
        {
            _periodTransactions = _periodTransactions ?? GetMyDataFromDB();
            return _periodTransactions;
        }
    }
_periodTransactions = GetMyDatFromDB();
RaisePropertyChanged(nameof(PeriodTransactions));