Calendar control width is too large at initialization of usercontrol - wpf

I have used calendar control and its display mode is year when my user control is loads then calender width s too large and doesn't appear when width is set .but when is select any month then width is set as it should
i m not getting what is the issue behind it
can anyone help me quickly???
Here is my code and what ever selected month will appear in textbox's text property and on click event of button my calendar control is set to visible
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="HorizontalHorizontalAlignment="Left "Margin="4">
<Label Content="Month: " HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" FontWeight="DemiBold" Margin="4"/>
<TextBox Height="23" HorizontalAlignment="Stretch" Name="SelectedMonthTextBox" VerticalAlignment="Top" Width="130" Margin="4" LostFocus="SelectedMonthTextBox_LostFocus" SelectionBrush="#FF767879"/>
<Button Content=".." Height="20" Width="20" Margin="4" Name="button" Click="button_Click" />
</StackPanel>
<Calendar Grid.Row="1" Grid.Column="0" Margin="50,-8" Panel.ZIndex="100" Name="calendar1" DisplayModeChanged="calendar1_DisplayModeChanged" DisplayDateChanged="calendar1_DisplayDateChanged" HorizontalAlignment="Left" DisplayMode="Year" Style="{StaticResource CalendarStyle}" Focusable="False" HorizontalContentAlignment="Left" VerticalAlignment="Top" />

i took a look your code and from information you provided so far, there is no any change a you describe. I believe that you should check following:
What happens in calendar1_DisplayModeChanged
How defined your style: StaticResource CalendarStyle
If it possible please post them here. I will try to help as soon as possible :)

Related

What does controls:TextBoxHelper.IsWaitingForData do?

I have a small WPF application that is using mahapp.metro for styling. There are 7 textboxes in the window with code similar to the examples below. When I run the application, it takes a lot of cpu, more than 90% if possible. When I deleted the controls:TextBoxHelper.IsWaitingForData="True", cpu usage dropped to almost 0% and I have not been able to find any issues. What does the TextBoxHelper.IsWaitingForData really mean?
<Canvas Grid.Column="0" Grid.Row="1">
<TextBox x:Name="SecurityStatusTextBox" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Text="{Binding SelectedSecurityStatus, Mode=OneWay}"
Margin="50,50,0,0" Height="50" Width="250" FontSize="16"
controls:TextBoxHelper.ClearTextButton="True"
controls:TextBoxHelper.IsWaitingForData="True"
controls:TextBoxHelper.UseFloatingWatermark="True"
controls:TextBoxHelper.Watermark="{x:Static properties:Resources.SecurityStatus}"/>
<TextBox x:Name="BagTypeTextBox" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Text="{Binding SelectedBagType, Mode=OneWay}"
Margin="325,50,0,0" Height="50" Width="250" FontSize="16"
controls:TextBoxHelper.ClearTextButton="True"
controls:TextBoxHelper.IsWaitingForData="True"
controls:TextBoxHelper.UseFloatingWatermark="True"
controls:TextBoxHelper.Watermark="{x:Static properties:Resources.BagType}"/>
</Canvas>
When the IsWaitingForData property is set to true an Effect is applied to the border of the TextBox and animation is applied to the Opacity property: https://github.com/MahApps/MahApps.Metro/blob/8a87a1b6ee7376e22930e465e8f3e85f4c5a73bc/src/MahApps.Metro/MahApps.Metro/Styles/Controls.TextBox.xaml
If you pay attention you will see that the TextBox gets a slight shadow that fades in and out when the IsWaitingForData property is set to true.
If this causes any issues for you should probably just set this property back to false.
You may also want to report the issue at GitHub: https://github.com/MahApps/MahApps.Metro/issues

Visual Studio 2010 XAML layout editor not WYSIWYG

When I'm creating WPF layouts in Visual Studio 2010 I just drag and drop controls from the Toolbox onto the default element, typically a Grid. When I do this the spacing shown in Visual studio does not match what's displayed at runtime. For example, here are the visual editor and runtime results, respectively, for two layouts . . .
. . . and . . .
... notice the spacing is altered.
Here's the XAML that was generated for the two examples . . .
<Button Content="ProjectPattern" Height="23" HorizontalAlignment="Left" Margin="12,296,0,0" Name="butProjPattern" VerticalAlignment="Top" Width="117" Click="butProjPattern_Click" />
<TextBlock Height="22" HorizontalAlignment="Left" Margin="135,0,0,141" Name="ResultProjPattern" Text="(result)" VerticalAlignment="Bottom" Width="41" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="175,295,0,0" Name="TextBlockPattern" Text=" (pattern file)" Padding="4" VerticalAlignment="Top" Width="135" Background="#FFF1F3FF" />
<Button Content="...browse" Height="23" HorizontalAlignment="Right" Margin="0,294,814,0" Name="buttonPatternBrowse" VerticalAlignment="Top" Width="54" Click="buttonPatternBrowse_Click" />
...and...
<Button Content="Display Cross" Height="24" HorizontalAlignment="Left" Margin="622,108,0,0" Name="buttonCross" VerticalAlignment="Top" Width="117" Click="buttonCross_Click" />
<TextBlock Height="24" HorizontalAlignment="Right" Margin="0,107,344,0" Name="ResultCross" Text="(result)" VerticalAlignment="Top" Width="100" />
<Button Content="Display Diamond" Height="23" HorizontalAlignment="Left" Margin="622,138,0,0" Name="butDiamond" VerticalAlignment="Top" Width="117" Click="butDiamond_Click" />
<TextBlock Height="23" HorizontalAlignment="Right" Margin="0,138,344,0" Name="ResultDiamond" Text="(result)" VerticalAlignment="Top" Width="100" />
<Button Content="DisplayFullField" Height="24" HorizontalAlignment="Left" Margin="622,165,0,0" Name="butFullField" VerticalAlignment="Top" Width="117" />
<TextBlock Height="24" HorizontalAlignment="Right" Margin="0,164,344,0" Name="ResultFullField" Text="(result)" VerticalAlignment="Top" Width="100" />
I added button handlers, text content, etc, but the layout is strictly what was generated by the Visual Studio layout editor. One thing I noticed is that even though it's drag-and-drop from the Toolbox onto the Grid, Visual Studio is inconsistent about whether it chooses "Left" or "Right" for its HorizontalAlignment for different elements.
The container element is just a default Grid in a default Window - it can minimize but not resize . . .
<Window x:Class="Caller1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="1200" ResizeMode="CanMinimize">
<Grid Height="460">
The result is that I have to waste lots of time going between the editor and running my app to "tweak" my layout. What prevents the "WYSIWYG" layout editor from matching the runtime layout and how can I get them to correspond?
Thanks in advance.
I can't see your images (dumb work firewall), but I can tell by your XAML that you are using the Grid wrong. I understand that this may be the fault of the editor, but seriously, you shouldn't be setting the Width and Height properties on all your elements.
If you want a fixed size for the Grid's children, set up some RowDefinitions and ColumnDefinitions and set those sizes. Your layout is much easier to work with (and will probably be more accurate in the designer) if you do this. So, your first Grid would look like this:
<Grid Margin="12,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="117" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="138" /> <!-- This is your desired width + horizontal margins -->
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button x:Name="butProjPattern"
Grid.Column="0"
Content="ProjectPattern"
VerticalAlignment="Center"
Click="butProjPattern_Click" />
<TextBlock x:Name="ResultProjPattern"
Grid.Column="1"
Margin="6,0,0,0"
Text="(result)"
VerticalAlignment="Center" />
<TextBlock x:Name="TextBlockPattern"
Grid.Column="2"
Margin="3,0,0,0"
Padding="4"
Text=" (pattern file)"
Background="#FFF1F3FF" />
<Button x:Name="buttonPatternBrowse"
Grid.Column="3"
Content="...browse"
Margin="22,0,0,0"
VerticalAlignment="Center"
Padding="2,1"
Click="buttonPatternBrowse_Click" />
</Grid>
The other thing I want to note is that after I put this in Kaxaml, which is great for rapid prototyping of your XAML layouts, I noticed that you should really just be using a StackPanel. The first grid could really be this much simpler implementation (you will have to set the Width on a couple of your items to match the layout):
<StackPanel Margin="12,0,0,0" Orientation="Horizontal">
<Button x:Name="butProjPattern"
Content="ProjectPattern"
Width="117"
Click="butProjPattern_Click" />
<TextBlock x:Name="ResultProjPattern"
Margin="6,0,0,0"
Text="(result)"
VerticalAlignment="Center" />
<TextBlock x:Name="TextBlockPattern"
Margin="3,0,0,0"
Padding="4"
Width="135"
Text=" (pattern file)"
Background="#FFF1F3FF" />
<Button x:Name="buttonPatternBrowse"
Content="...browse"
Margin="22,0,0,0"
VerticalAlignment="Center"
Padding="2,1"
Click="buttonPatternBrowse_Click" />
</StackPanel>
This answer is not directly an answer to your actual question, but instead a suggestion to avoid your problem.
You will often get UI control placement problems when you use exact control positioning with the Margin property. It is a good procedure (and in WPF especially) to allow controls to place and/or space themselves using the various Alignment options available. I have always managed to get the desired layout using this method.
For example, had you used a Grid or StackPanel to arrange your controls, you would not have come across this problem. Many WPF developers that I know don't even bother using the designer in Visual Studio because of the problems that you mentioned... after dragging and dropping a control and then going to the XAML to remove unwanted values, it often works out quicker to simply type the XAML myself.
If you really want to know why the Visual Studio designer... well, er... doesn't work properly, you may have more luck asking on the Microsoft Developer Forum, where you question might actually get seen by a Microsoft employee.

Silverlight 4: Dispatcher-error when using Calendar or DatePicker

I read so many good answers on this site, so I never had to ask something.
Now I have a problem I cant find a answer. I'm programming a Windows App für 7.1 and try to use a Calendar Control, I dont care that much if its Calendar or DatePicker.
But whenever I change the value there, I get the following error:
"This type of Collection does not support changes to its SourceCollection from a thread different from the Dispatcher thread."
Since im quite beginner, I dont do anything fancy with Threads and I didnt find anything about this error in the web, so I guess, this must be a unique error at my environment.
Has anyone a idea, where this error can come from?
Thanks for your responses
Müller Matthias
You should use DatePicker from SDK namespace,
http://samples.msdn.microsoft.com/silverlight/samplebrowser/#/?sref=System.Windows.Controls.Calendar
I can also suggest Telerik's silverlight controls
http://demos.telerik.com/silverlight/
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="CalendarExample.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="Trebuchet MS" FontSize="11"
Width="600" Height="750">
<StackPanel x:Name="LayoutRoot" Background="White">
<StackPanel Orientation="Horizontal" Margin="5" >
<!-- A basic Calendar. -->
<TextBlock Width="100"
Text="Calendar with date ranges defined:" TextWrapping="Wrap" Margin="5" />
<sdk:Calendar x:Name="cal" />
<!-- A Calendar does not highlight today's date. -->
<TextBlock Width="100"
Text="Calendar that does not highlight today's date:" TextWrapping="Wrap" Margin="5"/>
<sdk:Calendar x:Name="cal2" Canvas.Left="270" Canvas.Top="100"
IsTodayHighlighted="false" />
</StackPanel>
<!-- Two DatePicker controls, one using the default Short date format -->
<!-- and the other using the Long date format. -->
<StackPanel Orientation="Horizontal" >
<TextBlock Width="200" Margin="5"
Text="DatePicker with Short date format and Calendar event handlers:" TextWrapping="Wrap" />
<sdk:DatePicker x:Name="dp1" Height="20" Width="200" />
</StackPanel>
<StackPanel Orientation="Horizontal" >
<TextBlock Width="200" Margin="5"
Text="DatePicker with Long date format and DateSelected event handler:" TextWrapping="Wrap" />
<sdk:DatePicker Height="20" x:Name="dp2" SelectedDateFormat="Long" Width="200"/>
</StackPanel>
<!-- Output TextBlock -->
<TextBlock x:Name="text1" HorizontalAlignment="Left" Height="40" Width="350" Margin="5" />
<!-- A Calendar to demonstrate multiple selection. -->
<StackPanel Orientation="Horizontal" >
<TextBlock Width="200" Margin="5"
Text="Calendar with multiple selections and blackout dates:" TextWrapping="Wrap" />
<sdk:Calendar x:Name="cal3" />
</StackPanel>
</StackPanel>

Label Target="{Binding ElementName=UIName}" bug, or is it just me?

I've been tring to get this example to work, but there seems to be a bug in the code Label Target code, in that when you click on the second label the focus is set to the DatePicker rather than the TextBox, regardless which label you click first, also when you have entered the date the second label still sets it focus to DatePicker.
<!-- Unbound Date of Birth field -->
<sdk:Label Content="Date of Birth" IsRequired="True" Margin="5" />
<StackPanel Orientation="Horizontal" Grid.Column="1">
<sdk:DatePicker Height="23" />
<sdk:DescriptionViewer Description="Please enter your date of birth."/>
</StackPanel>
<!-- ID Number field -->
<sdk:Label Grid.Row="1" Margin="5" Target="{Binding ElementName=tbIdNumber}" />
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1">
<TextBox x:Name="tbIdNumber" Height="23" Width="100"
Text="{Binding IdNumber, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<sdk:DescriptionViewer Target="{Binding ElementName=tbIdNumber}"/>
</StackPanel>
I found this example here on the MSDN link text. To me this seems to be a bug, or am I just losing it?
Apologies if this is a duplicate question... I have googled this and all the examples seem to be the same, in that the target element is not being focused to.
It seems a reasonable assumption that clicking the label ought to move the focus to the associated control. However the Label does not actually provide that function.

Change content of ImageButton programatically (WPF)

I am using a ImageButton as
<Button Width="80" Height="25"
VerticalAlignment="Top"
HorizontalAlignment="Right"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Margin="0,0,1.5,0"
Name="btnActivate"
Click="OnActivate">
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="Shutdown.ico"
Width="12" Height="12"
Margin="0,0,5,0"/>
<TextBlock>Activate</TextBlock>
</StackPanel>
</Button>
I want to change the Content of button to 'Deactivate' when I click on it without changing the image. How can I do that? And also I need to some operations based on the content of button in my C# code.
You could just add a name to the TextBlock
<TextBlock Name="textBlock1">Activate</TextBlock>
and use it to modify the content in your OnActivate event handler
textBlock1.Text = "Deactivate";

Resources