Problem:
I was trying to get a button to include multiple lines. I tried using the declarative syntax in the button
<Button
Content="My Main Title \n Subtitle"/>
This did not work since this is going to render without the formatting implied inline.
Solution:
Really straight forward just add the textbox control into the button control and move on.
<Button
Command="{Binding MVVMControl}"
Name="MyFabulousButton">
<TextBlock
TextAlignment="Center"
TextWrapping="Wrap">
First Line
<Linebreak/>
Second Line
</TextBlock>
</Button>
Source
http://stackoverflow.com/questions/1449276/multline-text-in-a-wpf-button
This missive records my trials and tribulations as I code my way through projects. Fix the problem once and reuse the solution!
Showing posts with label XAML. Show all posts
Showing posts with label XAML. Show all posts
Monday, August 10, 2015
Wednesday, September 10, 2014
Xaml Binding EventToCommand to MVVM core
I have worked my way through Stuart Lodge's N+1 tutorials.
I was stumped about how to connect my Icommand control from the core to the UI.
Stuart's demo showed how to connect a button or checkbox to the MvxCommand but did not explain how to use the EventToCommand relationship.
I was trying to have a stackpanel fire the event when the user taps it in the UI.
The stackoverflow article provided me the leg up I needed.
I was struggling on which syntax to use to associate a stackpanel in xaml with my command. Turns out I needed to reference the Cirrious namespace in the xaml
xmlns:Commands="clr-namespace:Cirrious.MvvmCross.WindowsPhone.Commands;assembly=Cirriou... xmlns:Commands="clr-namespace:Cirrious.MvvmCross.WindowsPhone.Commands;assembly=Cirrious...
<i:Interaction.Triggers >
<i:EventTrigger EventName="Tap">
<Commands:MvxEventToCommand Command="{Binding OnItemTappedCommand}" />
</i:EventTrigger >
</i:Interaction.Triggers>
Source:
http://stackoverflow.com/questions/16823444/im-getting-build-errors-when-using-mvxeventtocommand-within-a-wp8-project
https://github.com/MvvmCross/MvvmCross/blob/e6d10972b5c28e00e80acc3d9e8910961aa813d6/Cirrious/Cirrious.MvvmCross.WindowsPhone/Commands/MvxEventToCommand.cs
I was stumped about how to connect my Icommand control from the core to the UI.
Stuart's demo showed how to connect a button or checkbox to the MvxCommand but did not explain how to use the EventToCommand relationship.
I was trying to have a stackpanel fire the event when the user taps it in the UI.
The stackoverflow article provided me the leg up I needed.
I was struggling on which syntax to use to associate a stackpanel in xaml with my command. Turns out I needed to reference the Cirrious namespace in the xaml
xmlns:Commands="clr-namespace:Cirrious.MvvmCross.WindowsPhone.Commands;assembly=Cirriou... xmlns:Commands="clr-namespace:Cirrious.MvvmCross.WindowsPhone.Commands;assembly=Cirrious...
<i:Interaction.Triggers >
<i:EventTrigger EventName="Tap">
<Commands:MvxEventToCommand Command="{Binding OnItemTappedCommand}" />
</i:EventTrigger >
</i:Interaction.Triggers>
Source:
http://stackoverflow.com/questions/16823444/im-getting-build-errors-when-using-mvxeventtocommand-within-a-wp8-project
https://github.com/MvvmCross/MvvmCross/blob/e6d10972b5c28e00e80acc3d9e8910961aa813d6/Cirrious/Cirrious.MvvmCross.WindowsPhone/Commands/MvxEventToCommand.cs
Friday, May 16, 2014
Conditional element in xaml depending on the binding content
Problem:
I was trying to hide and expose information in xaml form. The Address should not display if it is empty.
<TextBlock Text="{Binding Path=Address}" />
Solution:
<TextBlock Text="{Binding Path=Address}" Visibility="{Binding Path=Address, Converter={StaticResource StringLengthVisibilityConverter}" />
Create helper class and reference in the xaml.
public class StringLengthVisiblityConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || value.ToString().Length == 0)
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Don't need to implement this
}
}
Source:
http://stackoverflow.com/questions/4450866/conditional-element-in-xaml-depending-on-the-binding-content
I was trying to hide and expose information in xaml form. The Address should not display if it is empty.
<TextBlock Text="{Binding Path=Address}" />
Solution:
<TextBlock Text="{Binding Path=Address}" Visibility="{Binding Path=Address, Converter={StaticResource StringLengthVisibilityConverter}" />
Create helper class and reference in the xaml.
public class StringLengthVisiblityConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || value.ToString().Length == 0)
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Don't need to implement this
}
}
Source:
http://stackoverflow.com/questions/4450866/conditional-element-in-xaml-depending-on-the-binding-content
The property 'VisualTree' is set more than once
I was trying to design a template for binding a collection and was getting this error when I tried
to Bind text block to the property.
Problem:
I could not get this to compile it kept throwing the error. - The property 'VisualTree' is set more than once
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Grid.Column="0" Content="{Binding Path=Type}" />
<TextBox Grid.Column="1" Text="{Binding Path=CountryPrefix}" />
<TextBox Grid.Column="2" Text="{Binding Path=Number}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Solution:
I was missing the layout panel, ie. StackPanel
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Grid.Column="0" Content="{Binding Path=Type}" />
<TextBox Grid.Column="1" Text="{Binding Path=CountryPrefix}" />
<TextBox Grid.Column="2" Text="{Binding Path=Number}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
to Bind text block to the property.
Problem:
I could not get this to compile it kept throwing the error. - The property 'VisualTree' is set more than once
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Grid.Column="0" Content="{Binding Path=Type}" />
<TextBox Grid.Column="1" Text="{Binding Path=CountryPrefix}" />
<TextBox Grid.Column="2" Text="{Binding Path=Number}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Solution:
I was missing the layout panel, ie. StackPanel
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Grid.Column="0" Content="{Binding Path=Type}" />
<TextBox Grid.Column="1" Text="{Binding Path=CountryPrefix}" />
<TextBox Grid.Column="2" Text="{Binding Path=Number}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Tuesday, April 1, 2014
BitMapImage into a byte array - XAML manipulation
I was messing with a listbox control for my windows phone app and needed to convert a series
of bitmap images. Found this piece of code to convert the object. Now I should be able to
use my httppost class to pass the image as a parameter.
public static byte[] ConvertToBytes(this BitmapImage bitmapImage) {
byte[] data = null;
using (MemoryStream stream = new MemoryStream()) {
WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin); data = stream.GetBuffer();
}
return data;
}
Source:
http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array
of bitmap images. Found this piece of code to convert the object. Now I should be able to
use my httppost class to pass the image as a parameter.
public static byte[] ConvertToBytes(this BitmapImage bitmapImage) {
byte[] data = null;
using (MemoryStream stream = new MemoryStream()) {
WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
stream.Seek(0, SeekOrigin.Begin); data = stream.GetBuffer();
}
return data;
}
Source:
http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array
Wednesday, March 19, 2014
Weeeee fun with the ListPicker control Windows Phone
I was trying to get this control to behave. I was using the SelectionChanged event thinking it
would behave like the trusty combo box. This is not the case. The ListPicker is "always on"
so it will fire when the page loads or say other events like... setting the visibility to true.
This is what bit me. Fortunately, the solution to this is quite simple.
Solution:
private void SomeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Make sure we don't handle the event during initiation.
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
if (this.SomeListPicker.SelectedItem != null)
{
// Do actual stuff.
}
}
}
Source:
http://samidipbasu.com/2011/05/19/fun-with-toolkit-listpicker-selections/
would behave like the trusty combo box. This is not the case. The ListPicker is "always on"
so it will fire when the page loads or say other events like... setting the visibility to true.
This is what bit me. Fortunately, the solution to this is quite simple.
Solution:
private void SomeListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Make sure we don't handle the event during initiation.
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
if (this.SomeListPicker.SelectedItem != null)
{
// Do actual stuff.
}
}
}
Source:
http://samidipbasu.com/2011/05/19/fun-with-toolkit-listpicker-selections/
XAML Rotate Image
Trying to spice up a select activity on a phone app. I had a png image which I wanted to rotate 90'
upon selection. Turns out that the API's for this are pretty straight forward. I did this in the code behind wired up to the selectionchanged event.
XAML:
<Image x:Name="imgIcon" Tap="OnTap"/>
Code Behind:
OnTap(object sender, RoutedEventArgs e)
{
SelectEditIcon(imgIcon);
}
private void SelectEditIcon(Image image)
{
RotateTransform rt = new RotateTransform();
rt.Angle = 90;
image.RenderTransform = rt;
image.RenderTransformOrigin = new Point(0.5,0.5);
}
private void UnselectEditIcon(Image image)
{
RotateTransform rt = new RotateTransform();
rt.Angle = 360;
image.RenderTransform = rt;
image.RenderTransformOrigin = new Point(0.5,0.5);
}
Note: It is important to define the point of rotation for the image if you do not then it will not
rotate correctly. This was done by using setting the Origin to X=0.5/ Y=0.5
source:
http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform(v=vs.110).aspx
upon selection. Turns out that the API's for this are pretty straight forward. I did this in the code behind wired up to the selectionchanged event.
XAML:
<Image x:Name="imgIcon" Tap="OnTap"/>
Code Behind:
OnTap(object sender, RoutedEventArgs e)
{
SelectEditIcon(imgIcon);
}
private void SelectEditIcon(Image image)
{
RotateTransform rt = new RotateTransform();
rt.Angle = 90;
image.RenderTransform = rt;
image.RenderTransformOrigin = new Point(0.5,0.5);
}
private void UnselectEditIcon(Image image)
{
RotateTransform rt = new RotateTransform();
rt.Angle = 360;
image.RenderTransform = rt;
image.RenderTransformOrigin = new Point(0.5,0.5);
}
Note: It is important to define the point of rotation for the image if you do not then it will not
rotate correctly. This was done by using setting the Origin to X=0.5/ Y=0.5
source:
http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform(v=vs.110).aspx
Friday, March 14, 2014
XAML Listbox control
I was trying to get a windows phone control to show a horizontal scrolling picture bar.
The pictures should be picked up from the camera roll in Windows Phone 8.
This does not exist out of the box. It took some XAML searching and programming
to get it working correctly. Thanks to Andy_TeamG for the tip!
XAML:
<Image x:Name="Image" />
Code Behind
private void GetImages()
Source:
http://andy-teamg.blogspot.com/2010/07/windows-phone-7-selecting-device-photos.html
The pictures should be picked up from the camera roll in Windows Phone 8.
This does not exist out of the box. It took some XAML searching and programming
to get it working correctly. Thanks to Andy_TeamG for the tip!
XAML:
<Image x:Name="Image" />
<ListBox.ItemTemplate OnSelectionChanged="lstImages_SelectionChanged">
<DataTemplate>
<Image Source="{Binding ImageFile}" Width="100" Height="100"/>
DataTemplate>
ListBox.ItemTemplate>
Code Behind
private void GetImages()
{
MediaLibrary mediaLibrary = new MediaLibrary();
var pictures = mediaLibrary.Pictures;
foreach (var picture in pictures)
{
BitmapImage image = new BitmapImage();
image.SetSource(picture.GetImage());
MediaImage mediaImage = new MediaImage();
mediaImage.ImageFile = image;
lstImages.Items.Add(mediaImage);
}
private void lstImages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MediaLibrary mediaLibrary = new MediaLibrary();
BitmapImage image = new BitmapImage();
image.SetSource(mediaLibrary.Pictures[lstImages.SelectedIndex].GetImage());
imgSelectedPhoto.Source = image;
}
Source:
http://andy-teamg.blogspot.com/2010/07/windows-phone-7-selecting-device-photos.html
Subscribe to:
Posts (Atom)