【问题标题】:Binding to nested Listbox SelectedItem in Custom Control绑定到自定义控件中的嵌套列表框 SelectedItem
【发布时间】:2016-10-22 21:23:24
【问题描述】:

我已经搜索了一段时间,但无法找到解决方案。

我的目标是将 ViewModel 中的属性绑定到自定义控件中列表框的选定项。

首先 - 此日历控件来自 Jarloo Custom Calendar。我只是为了定制个人用途而对其进行了修改。

日历控件(略)如下:

    <ResourceDictionary>
        <Style TargetType="{x:Type Neptune:Calendar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Neptune:Calendar}">

                    <!--Some header items and other misc controls....-->

                        <DockPanel>

                            <!--Calendar-->
                            <ListBox ItemsSource="{Binding Days}" Background="{x:Null}" 
                            SelectedItem="{Binding SelectedDay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

                        </DockPanel>  
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


和本地(缩写):

    public class Calendar : Control
    {
        public ObservableCollection<Day> Days { get; set; }

        //SelectedDay Property
        public static readonly DependencyProperty SelectedDayProperty =
        DependencyProperty.Register("SelectedDay", typeof(Day), typeof(Calendar),
        new PropertyMetadata(null, OnSelectedDayChanged));

        public Day SelectedDay
        {
            get { return (Day)GetValue(SelectedDayProperty); }
            set { SetValue(SelectedDayProperty, value); }
        }

        private static void OnSelectedDayChanged(DependencyObject pager, DependencyPropertyChangedEventArgs e)
        {
            Calendar d = pager as Calendar;
            //MessageBox.Show(d.SelectedDaDateString());///THIS SHOWS CORRECT SELECTED DATE!!!!
            d.SetValue(ThisDayProperty, d.SelectedDay);
        }

在使用此控件的窗口中,我尝试绑定到此 SelectedDay 属性,甚至制作了另一个 DP 将值传递给仅用于测试。两个值都没有正确绑定。

   <Neptune:Calendar Grid.Row="1" x:Name="Calendar" Margin="0,10,0,0" 
   ThisDay="{Binding DaySelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
    RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">


我不明白...为什么这不起作用?
我想要做的就是公开一个属性,该属性反映了 Days 日历列表中的选定项目。

【问题讨论】:

    标签: c# wpf mvvm custom-controls dependency-properties


    【解决方案1】:
    猜你喜欢
    • 2017-10-26
    • 1970-01-01
    • 2015-07-23
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2018-01-17
    • 2014-01-05
    相关资源
    最近更新 更多