WPF应该怎样获取该控件

夏天的枫 2016-08-26 10:20:33

<Grid>
<dxe:PopupBaseEdit HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" MaskType="DateTime" Mask="yyyy-MM">
<dxe:PopupBaseEdit.PopupContentTemplate>
<ControlTemplate>
<local:CustomDateNavigator x:Name="dateNavigator1" FocusedDate="{Binding Path=(dxe:BaseEdit.OwnerEdit).EditValue, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
</ControlTemplate>
</dxe:PopupBaseEdit.PopupContentTemplate>
</dxe:PopupBaseEdit>
<Button Click="Button_Click"/>
</Grid>

这个是去找的DEV的一个只能选择年月的示例,当我点击button的时候,怎么获取到dateNavigator1,想过用TAG,不过没有成功,MVVM模式,望赐教
...全文
227 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ouylvr 2017-07-04
  • 打赏
  • 举报
回复
xaml代码: <Button Width="100" Command="{Binding BtnClickCommand}" CommandParameter="{Binding ElementName=dateNavigator1}" />   ViewModel:   private DelegateCommand<DateNavigator> BtnClickCommand; //申明委托   BtnClickCommand = new DelegateCommand<DateNavigator>(BtnClick); //实例化   private void BtnClick(DateNavigator dn)   {     string strBtnName = dn.Name;//使用   }
夏天的枫 2016-08-26
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
CurrenDateText 依赖项属性,而非 普通的。net 属性?
不对,是依赖项属性
夏天的枫 2016-08-26
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
CurrenDateText 依赖项属性,而非 普通的。net 属性?
不是依赖项属性
exception92 2016-08-26
  • 打赏
  • 举报
回复
CurrenDateText 依赖项属性,而非 普通的。net 属性?
夏天的枫 2016-08-26
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
是 VisualTreeHelper

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            string btnName = "";
            List<DateNavigator> btnList = FindVisualChild<DateNavigator>((bt.Parent as Grid));
            foreach (DateNavigator item in btnList)
            {
                btnName += string.IsNullOrEmpty(btnName) ? item.Name.ToString() : "," + item.Name.ToString();
            }
            MessageBox.Show(string.Format((bt.Parent as Grid).Name.ToString() + "共有{0}个,名称分别为{1}", btnList.Count, btnName));
        }
        List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            try
            {
                List<T> TList = new List<T> { };
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        TList.Add((T)child);
                    }
                    else
                    {
                        List<T> childOfChildren = FindVisualChild<T>(child);
                        if (childOfChildren != null)
                        {
                            TList.AddRange(childOfChildren);
                        }
                    }
                }
                return TList;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return null;
            }
        }
获取不了
夏天的枫 2016-08-26
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
是 VisualTreeHelper
而且很奇怪的事情是我local里面的DateNavigator在我button所在xaml(第一段代码)中找不到CurrenDateText属性。。。
exception92 2016-08-26
  • 打赏
  • 举报
回复
是 VisualTreeHelper
exception92 2016-08-26
  • 打赏
  • 举报
回复
VisualHelper 不行?
夏天的枫 2016-08-26
  • 打赏
  • 举报
回复
附上local;

<dxe:DateNavigator x:Class="FProject.CustomDateNavigator"
             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:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             CalendarView="Years" 
             ShowTodayButton="False"
             />
夏天的枫 2016-08-26
  • 打赏
  • 举报
回复

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧