使用MVVM Light工具包发行MonthPicker大小 [英] Issue MonthPicker Size with MVVM Light toolkit

查看:86
本文介绍了使用MVVM Light工具包发行MonthPicker大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的月份选择器存在一个奇怪的问题。
初始化时宽度很大。



我正在使用MVVM Light Toolkit,看来是引起问题的原因。



实际上,对于标准WPF应用程序,相同的代码有效...



另一个提示,没有弹出控件,该代码与MVVM一起工作轻工具包。



这是我的代码:

 < 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>

<日历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>



这是后面的代码:

 使用System.Windows; 
使用MvvmLight1.ViewModel;
使用System.Windows.Controls;

命名空间MvvmLight1
{
公共部分类MainWindow:窗口
{
public MainWindow()
{
InitializeComponent() ;
}
private void _calendar_DisplayModeChanged(object sender,CalendarModeChangedEventArgs e)
{
_calendar.DisplayMode = CalendarMode.Year;

}
private void _calendar_OnLoaded(对象发送者,RoutedEventArgs e)
{
_calendar.DisplayMode = CalendarMode.Year;
}
}
}

结果如下:



没有什么花哨的东西了……我现在正苦苦挣扎。会很感激!



谢谢!

解决方案

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



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



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

 < StackPanel Orientation = Horizo​​ntal 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>
<日历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 =填充 Visual = {DynamicResource appbar_calendar} />
< /Rectangle.OpacityMask>
< / Rectangle>
< / ToggleButton>
< / StackPanel>

这是后面的代码:

  private void _calendar_DisplayDateChanged(object sender,CalendarDateChangedEventArgs e)
{
//如果用户单击日历按钮以更改年份,则日历必须保持打开状态$ b 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 
{

公共对象Convert(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
return((DateTime)value).ToString( MMMM yyyy);
}

公共对象ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
throw new NotImplementedException();
}
}

我希望它对某人有用! / p>

I've a strange issue with my monthpicker. It has a huge width at initialization.

I'm using MVVM Light Toolkit and it seems that's causing the issue.

Indeed, with a standard WPF application, the same code works...

Another hint, without the popup control, this code works with MVVM Light Toolkit.

Here is my code:

<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>

And here is the code Behind:

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;
        }
    }
}

And here the result:

Nothing fancy... I'm struggling with it for a while now.. Any help would be appreciated!

Thanks in advance!

解决方案

Ok so I've found a solution to my issue. I've removed the on_Loaded and the DisplayModeChanged method of the calendar and put its content into the popup opened event.

Here is the full code for the month picker.

The xaml code (the ressources for the button are from 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>

Here is the code behind:

    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;
    }

And the converter:

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();
    }
}

I hope it'll be usefull for someone!

这篇关于使用MVVM Light工具包发行MonthPicker大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆