2016-11-08 10 views
0

私の奇妙な問題は私のmonthpickerです。 初期化時に大きな幅があります。問題MonthPickerのサイズがMVVM Lightツールキット

私はMVVM Light Toolkitを使用していますが、これが問題の原因です。標準のWPFアプリケーション、同じコードが動作すると確かに

、...

もう一つのヒントは、ポップアップ制御せずに、このコードはMVVMライトツールキットで動作します。ここで

は私のコードです:

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

そして、ここでの結果:

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

そしてここでは、背後にあるコードである enter image description here

空想何も...私はしばらくの間それと苦労している..どんな助けも認められるだろうiated!

ありがとうございます!

答えて

0

だから私は私の問題の解決策を見つけました。 カレンダーのon_LoadedメソッドとDisplayModeChangedメソッドを削除し、その内容をポップアップオープンイベントに入れました。

月のピッカーの完全なコードは次のとおりです。

XAMLコード(ボタンのressourcesがMahappsメトロからのものである):ここ

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

私はそれが誰かにとって役に立つことを願っています!

関連する問題