wpf DatePicker模板,绑定到SelectedDate无效

q0qdq0h2  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(215)

我正在尝试更改DatePicker模板。我几乎是在Custom Control的帮助下完成设计的。但是我不仅花了2天的时间在这上面,而且它也没有错误地工作。SelectedDateChenged调用更改事件被调用了多次。我决定改变方法,重写模板。现在有一个问题,即绑定不工作。即,在日历中有变化。我看到他们,事件被调用,SelectedDate在那里变化,但是绑定不起作用。请帮助我弄清楚,我附上模板和转换器的源代码。
日期选取器模板

<Style x:Key="DatePickerStyle" TargetType="{x:Type DatePicker}">
        <Style.Resources>
            <conv:MonthToGenitiveMonthNameConverter x:Key="MonthToGenitiveMonthNameConverter"/>
            <conv:MonthToAbbreviatedMonthNameConverter x:Key="MonthToAbbreviatedMonthNameConverter"/>
            <conv:DayToDayNameConverter x:Key="DayToDayNameConverter"/>
            <conv:DateToTwoDigitDateConverter x:Key="DateToTwoDigitDateConverter"/>
        </Style.Resources>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Height" Value="70"/>
        <Setter Property="Width" Value="500"/>
        <Setter Property="IsTodayHighlighted" Value="True"/>
        <Setter Property="SelectedDateFormat" Value="Short"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DatePicker}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <Grid x:Name="PART_Root" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <Grid  HorizontalAlignment="Stretch">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                    <TextBlock Margin="5 0 0 0" Foreground="{TemplateBinding Foreground}" FontSize="50" Text="{Binding Path=SelectedDate.Day, Converter={StaticResource DateToTwoDigitDateConverter}}" />
                                    <StackPanel Margin="10,2,0,2" VerticalAlignment="Center" >
                                        <TextBlock Foreground="{TemplateBinding Foreground}" FontSize="24" FontWeight="Bold" Text="{Binding Path=SelectedDate.Month, Converter={StaticResource MonthToGenitiveMonthNameConverter}}"/>
                                        <TextBlock Foreground="{TemplateBinding Foreground}" FontSize="14" Text="{Binding Path=SelectedDate.DayOfWeek, Converter={StaticResource DayToDayNameConverter}}"/>
                                    </StackPanel>
                                    <TextBlock Margin="5 0 0 0" Foreground="{TemplateBinding Foreground}" FontSize="50" Text="{Binding Path=SelectedDate.Year}" />
                                    <TextBlock Margin="5 0 0 0" Foreground="{TemplateBinding Foreground}" FontSize="50" Text="г." />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                                    <Button Name="PART_Button" Style="{StaticResource ExtendedDataPickerButtonStyle}">
                                        <fa:IconImage Icon="Calendar"  Style="{StaticResource BigIconStyle}"/>
                                    </Button>
                                    <DatePickerTextBox x:Name="PART_TextBox" Visibility="Collapsed"/>
                                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_Button}" StaysOpen="False" 
                                           ui:PopupProperties.HorizontalPlacementAlignment="Center"
                                           ui:PopupProperties.VerticalOffset="2"/>
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </Border>
                   
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

转换器
x一个一个一个一个一个x一个一个二个一个x一个一个三个一个x一个x一个x一个x一个x一个x一个
我不知道还能尝试什么。
从这个网站的代码,为此非常感谢,我已经检查,以防万一,它不在它.

ui:PopupProperties.HorizontalPlacementAlignment="Center"
ui:PopupProperties.VerticalOffset="2"

我正在尝试重新制作DatePicker和Calendar,我没有权限访问源代码中的Internal方法,而且有不少Internal方法,我可以发布上一次迭代编程的源代码,但结果是一个非常大的堆,功能被截断。

ubof19bj

ubof19bj1#

我还是解决了这个问题。我不会公布整个代码,我只会公布重大的变化。首先,自定义控件再次出现,只是非常简约。

public class ModernDatePicker : DatePicker, IDisposable
{
    public static readonly DependencyProperty VisualizatedDateProperty = DependencyProperty.Register(nameof(VisualizatedDate), typeof(DateTime), typeof(ModernDatePicker),
                           new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public DateTime VisualizatedDate
    {
        get { return (DateTime)GetValue(VisualizatedDateProperty); }
        set { SetValue(VisualizatedDateProperty, value); }
    }

    static ModernDatePicker()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ModernDatePicker), new FrameworkPropertyMetadata(typeof(ModernDatePicker)));
    }

    public ModernDatePicker()
    {
        Loaded += ModernDatePicker_Loaded;
        SelectedDateChanged += ModernDatePicker_SelectedDateChanged;
    }

    private void ModernDatePicker_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.SelectedDate == null)
        {
            SelectedDate = VisualizatedDate;
        }
    }

    private void ModernDatePicker_SelectedDateChanged(object? sender, SelectionChangedEventArgs e)
    {
        ModernDatePicker mdp = (ModernDatePicker)sender;
        if (mdp.SelectedDate != null)
        {
            VisualizatedDate = mdp.SelectedDate.Value;
        }
    }

}

    public void Dispose()
    {
        SelectedDateChanged -= ModernDatePicker_SelectedDateChanged;
        Loaded -= ModernDatePicker_Loaded;
    }

SelectDate是一个DateTime?类型,它不允许你在启动时立即获得值,你必须使用一个DependencyProperty,一个DateTime类型,以便能够使用绑定。

<Style TargetType="{x:Type cc:ModernDatePicker}">
        <Style.Resources>
            <conv:MonthToGenitiveMonthNameConverter x:Key="MonthToGenitiveMonthNameConverter"/>
            <conv:DayToDayNameConverter x:Key="DayToDayNameConverter"/>
            <conv:DateToTwoDigitDateConverter x:Key="DateToTwoDigitDateConverter"/>
        </Style.Resources>
        <Setter Property="Foreground" Value="#223266"/>
        <Setter Property="IsTodayHighlighted" Value="True"/>
        <Setter Property="CalendarStyle" Value="{DynamicResource ModernCalendarStyle}"/>
        <Setter Property="SelectedDateFormat" Value="Short"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="SubForeground" Value="#9eabe2"/>
        <Setter Property="OtherForeground" Value="White"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type cc:ModernDatePicker}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <Grid x:Name="PART_Root" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <Grid  HorizontalAlignment="Stretch">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                    <TextBlock Margin="5 0 0 0" Foreground="{TemplateBinding OtherForeground}" FontSize="50" Text="{Binding VisualizatedDate.Day, RelativeSource={RelativeSource AncestorType={x:Type cc:ModernDatePicker}},Converter = {StaticResource DateToTwoDigitDateConverter}}" />
                                    <StackPanel Margin="10,2,0,2" VerticalAlignment="Center" >
                                        <TextBlock Foreground="{TemplateBinding OtherForeground}" FontSize="24" FontWeight="Bold" Text="{Binding VisualizatedDate.Month, RelativeSource={RelativeSource AncestorType={x:Type cc:ModernDatePicker}}, Converter={StaticResource MonthToGenitiveMonthNameConverter}}"/>
                                        <TextBlock Foreground="{TemplateBinding SubForeground}" FontSize="14" Text="{Binding VisualizatedDate.DayOfWeek, RelativeSource={RelativeSource AncestorType={x:Type cc:ModernDatePicker}}, Converter={StaticResource DayToDayNameConverter}}"/>
                                    </StackPanel>
                                    <TextBlock Margin="5 0 0 0" Foreground="{TemplateBinding OtherForeground}" FontSize="50" Text="{Binding VisualizatedDate.Year, RelativeSource={RelativeSource AncestorType={x:Type cc:ModernDatePicker}}}" />
                                    <TextBlock Margin="5 0 0 0" Foreground="{TemplateBinding OtherForeground}" FontSize="50" Text="г." />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                                    <Button Name="PART_Button" Style="{StaticResource ExtendedDataPickerButtonStyle}">
                                        <fa:IconImage Icon="Calendar"  Style="{StaticResource BigIconStyle}"/>
                                    </Button>
                                    <DatePickerTextBox x:Name="PART_TextBox" Visibility="Collapsed"/>
                                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_Button}" StaysOpen="False" 
                                           ui:PopupProperties.HorizontalPlacementAlignment="Center"
                                           ui:PopupProperties.VerticalOffset="2"/>
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>

事实上,通过类比

相关问题