XAML WinUI 3/WASDK 1.3如何缩短DatePicker控件上的月份宽度?

px9o7tmv  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(139)

DatePicker
我想看看我能在这里使用什么:https://github.com/microsoft/microsoft-ui-xaml/blob/main/dev/CommonStyles/DatePicker_themeresources.xaml但它只是把我弄糊涂了。先谢谢你了!

pw9qyyiw

pw9qyyiw1#

你可以这样试试。

MainPage.xaml

<DatePicker x:Name="DatePickerControl" />

MainPage.xaml.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.DatePickerControl.LayoutUpdated += DatePickerControl_LayoutUpdated;
    }

    private void DatePickerControl_LayoutUpdated(object? sender, object e)
    {
        double monthControlWidth = 70;

        if (this.DatePickerControl.FindDescendant<Grid>(x => x.Name is "FlyoutButtonContentGrid") is Grid flyoutButtonContentGrid &&
            flyoutButtonContentGrid.ColumnDefinitions.Count > 0)
        {
            flyoutButtonContentGrid.ColumnDefinitions[0].Width = new GridLength(monthControlWidth, GridUnitType.Star);
        }

        if (VisualTreeHelper.GetOpenPopupsForXamlRoot(this.DatePickerControl.XamlRoot)
            .FirstOrDefault() is Popup popup &&
            popup.Child is DatePickerFlyoutPresenter datePickerFlyoutPresenter &&
            datePickerFlyoutPresenter.FindDescendant<Grid>(x => x.Name is "PickerHostGrid") is Grid pickerHostGrid &&
            pickerHostGrid.ColumnDefinitions.Count > 0)
        {
            pickerHostGrid.ColumnDefinitions[0].Width = new GridLength(monthControlWidth, GridUnitType.Star);
        }
    }
}

您可以在generic.xaml中找到每个元素的名称。

相关问题