WPF Binding to DependencyProperty of UserControl in DataTemplate is not working

时光总嘲笑我的痴心妄想 提交于 2019-12-13 18:24:16

问题


i have a custom userControl with DPs and have the problem that the binding to these properties only works if i use the controls outside of datatemplates.

Outside of Datatemplates works the usercontrol great.

XAML to Test the UserControl in a DataTemplate

        <GroupBox Header="DataTemplate" Padding="5">

            <ItemsControl ItemsSource="{Binding Dummies}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="local:Dummy">
                        <StackPanel Orientation="Horizontal" Margin="2">

                            <common:QuarterPicker SelectedFirstDay="{Binding Gueltigkeit}" Margin="5,0" />

                            <!--control the value of the item-->
                            <TextBlock Text="Gueltigkeit: "/>
                            <TextBlock Text="{Binding Gueltigkeit}"/>

                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>

        </GroupBox>

MainWindow codebehind and ViewModel

public partial class QuarterPickerTest : UserControl
{
    public QuarterPickerTest()
    {
        InitializeComponent();

        this.DataContext = new ViewModel();
    }
}

public class ViewModel
{
    public ViewModel()
    {
        this.Dummy = new Dummy { Gueltigkeit = DateTime.Today };

        this.Dummies = new List<Dummy>
            {
                new Dummy {Gueltigkeit = DateTime.Today},
                new Dummy {Gueltigkeit = DateTime.Today.AddMonths(6)},
                new Dummy {Gueltigkeit = DateTime.Today.AddDays(-6)},
            };
    }

    public Dummy Dummy { get; set; }

    public List<Dummy> Dummies { get; set; }
}

Here is the code behind of my UserControl

#region SelectedFirstDay

    public DateTime SelectedFirstDay
    {
        get { return (DateTime)GetValue(SelectedFirstDayProperty); }
        set { SetValue(SelectedFirstDayProperty, value); }
    }

    public static readonly DependencyProperty SelectedFirstDayProperty
        = DependencyProperty.Register("SelectedFirstDay", typeof (DateTime), typeof (QuarterPicker),
                                      new FrameworkPropertyMetadata(DateTime.Today, SelectedFirstDayPropertyChangedCallback) { BindsTwoWayByDefault = true });

    private static void SelectedFirstDayPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        var quarterPicker = dependencyObject as QuarterPicker;
        var date = (DateTime)args.NewValue;
        var quarter = GetQuarter(date);

        quarterPicker.UpdateProperties(date.Year, quarter);

        if (date.Year == quarterPicker.LeftInfiniteYear && quarter == quarterPicker.LeftInfiniteQuarter)
            quarterPicker.ShowPopupButtonLeftInfinite();
    }

    #endregion

Thanks for any suggestions!

来源:https://stackoverflow.com/questions/16293430/wpf-binding-to-dependencyproperty-of-usercontrol-in-datatemplate-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!