使用 MVVM Light 工具包发布 MonthPicker Size

Issue MonthPicker Size with MVVM Light toolkit

我的月份选择器有一个奇怪的问题。 它在初始化时具有巨大的宽度。

我正在使用 MVVM Light Toolkit,这似乎是导致问题的原因。

的确,对于标准的 WPF 应用程序,相同的代码可以工作...

另一个提示,没有弹出控件,此代码适用于 MVVM Light Toolkit。

这是我的代码:

<Window x:Class="MvvmLight1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    SizeToContent="WidthAndHeight"
    Title="MVVM Light Application"
    DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>

        <Popup IsOpen="{Binding ElementName=btn, Path=IsChecked}" StaysOpen="False" >

            <Calendar  x:Name="_calendar" 
                       Loaded="_calendar_OnLoaded" 
                       DisplayModeChanged="_calendar_DisplayModeChanged" 
                       DisplayMode="Month"  />
        </Popup>
        <ToggleButton  Height="50" Width="100" Content="Click me" x:Name="btn" ClickMode="Release"/>
</Grid>

下面是代码:

using System.Windows;
using MvvmLight1.ViewModel;
using System.Windows.Controls;

namespace MvvmLight1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void _calendar_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
        {
            _calendar.DisplayMode = CalendarMode.Year;

        }
        private void _calendar_OnLoaded(object sender, RoutedEventArgs e)
        {
            _calendar.DisplayMode = CalendarMode.Year;
        }
    }
}

结果如下:

没什么特别的...我现在正在努力解决这个问题..任何帮助将不胜感激!

提前致谢!

好的,我找到了解决问题的方法。 我删除了日历的 on_Loaded 和 DisplayModeChanged 方法,并将其内容放入弹出窗口打开事件中。

这是月份选择器的完整代码。

xaml代码(按钮资源来自Mahapps Metro):

    <StackPanel Orientation="Horizontal" Grid.Row="1">
        <Label  Width="100" Height="25" Content="{Binding DateCalendar, Converter={StaticResource MonthConverter} }"/>
        <Popup IsOpen="{Binding ElementName=btn, Path=IsChecked}" StaysOpen="False" Opened="Popup_Opened" PlacementTarget="{Binding ElementName=btn}" Placement="Right" >
            <Calendar  x:Name="_calendar" 
                           DisplayDate="{Binding DateCalendar}"
                           DisplayDateChanged="_calendar_DisplayDateChanged" 
                           DisplayMode="Month"/>
        </Popup>
        <ToggleButton Style="{StaticResource CircleButton}"  x:Name="btn" ClickMode="Release" >
            <Rectangle Width="16" Height="16" Fill="Black">
                <Rectangle.OpacityMask>
                    <VisualBrush Stretch="Fill"  Visual="{DynamicResource appbar_calendar}" />
                </Rectangle.OpacityMask>
            </Rectangle>
        </ToggleButton>
    </StackPanel>

下面是代码:

    private void _calendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
    {
    //If the user click the button of the calendar to change year, the calendar must remains open
        if (e.RemovedDate.HasValue && e.AddedDate.HasValue)
        {
            if (e.RemovedDate.Value.Year == e.AddedDate.Value.Year)
            {
                btn.IsChecked = false;
            }
        }
    }

    private void Popup_Opened(object sender, EventArgs e)
    {
        _calendar.DisplayMode = CalendarMode.Year;
    }

和转换器:

class FullDateToMonthConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((DateTime)value).ToString("MMMM yyyy");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我希望它对某人有用!