Items in ItemsControl takes width of First Item - xaml

WrapGrid in ItemsControl takes the width of the first item as the default width of all children items. Is there any workaround to make the itemwidth stretch according to each individual item's content?
Here's My XAML.
<Grid Grid.Row="2">
<ItemsControl Grid.Column="1" ItemsSource="{x:Bind articleTags}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="#E1E1E1" HorizontalAlignment="Stretch" Background="#E1E1E1" Margin="4,4,0,0">
<TextBlock Text="{Binding NAME}" Margin="6,0,6,0"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Screenshots:
In the second image the 'newsletters' item is not being fully displayed as it takes the width of the first item.
How to solve this?

Change the WrapGrid to ItemsStackPanel
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
UPDATE
Try to use custom wrap grid panel according to this sample
public class CustomWrapPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
// Just take up all of the width
Size finalSize = new Size { Width = availableSize.Width };
double x = 0;
double rowHeight = 0d;
foreach (var child in Children)
{
// Tell the child control to determine the size needed
child.Measure(availableSize);
x += child.DesiredSize.Width;
if (x > availableSize.Width)
{
// this item will start the next row
x = child.DesiredSize.Width;
// adjust the height of the panel
finalSize.Height += rowHeight;
rowHeight = child.DesiredSize.Height;
}
else
{
// Get the tallest item
rowHeight = Math.Max(child.DesiredSize.Height, rowHeight);
}
}
// Add the final height
finalSize.Height += rowHeight;
return finalSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
Rect finalRect = new Rect(0, 0, finalSize.Width, finalSize.Height);
double rowHeight = 0;
foreach (var child in Children)
{
if ((child.DesiredSize.Width + finalRect.X) > finalSize.Width)
{
// next row!
finalRect.X = 0;
finalRect.Y += rowHeight;
rowHeight = 0;
}
// Place the item
child.Arrange(new Rect(finalRect.X, finalRect.Y, child.DesiredSize.Width, child.DesiredSize.Height));
// adjust the location for the next items
finalRect.X += child.DesiredSize.Width;
rowHeight = Math.Max(child.DesiredSize.Height, rowHeight);
}
return finalSize;
}
}

Related

In Avalonia or Xaml in general, how would I implement the ability to use percentage based width or heights in controls

I find the grid control to be very messy, counter-intuitive, verboose, and breaking the idea of xml that position in the document is important to layout. I spent a lot of time programming in the Adobe Flex framework and found I was incredibly fast at UI development with that ability, and the UI is way easier to parse later on as well to update and maintain. With that in mind how do we bring the ability to make controls like stackpanel, and button that can tolerate percentage widths and heights?
Documenting this here so it might help someone. I came from Adobe Flex, and using percentage based widths and heights is a breeze and I find the grid control to be messy and ruins half of the point of using XML to define a UI by breaking the layout order and style and adds a lot of code for little value. Here is an example:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:s="clr-namespace:Sandbox.Spark"
x:Class="Sandbox.MainWindow" Padding="5">
<s:VGroup>
<Border Background="LightBlue" CornerRadius="5" PercentHeight="30" PercentWidth="50">
<Button Content="Test" HorizontalAlignment="Center"/>
</Border>
<Border Background="Green" CornerRadius="5" Height="200" PercentWidth="75" Padding="5">
<s:VGroup>
<Button Content="Test5" PercentWidth="50"/>
<Button Content="Test8"/>
</s:VGroup>
</Border>
<Border Background="LightGray" CornerRadius="5" PercentHeight="100" PercentWidth="100">
<s:HGroup>
<Button Content="Test2"/>
<Button Content="Test3"/>
</s:HGroup>
</Border>
</s:VGroup>
</Window>
I Created the classes Group, VGroup, and HGroup, which are similar to StackPanel's but better suited to dealing with percentage based layout. Here they are:
/// <summary>
/// A Panel control similar to StackPanel but with greater support for PercentWidth and PercentHeight
/// </summary>
public class Group : Panel
{
public static readonly StyledProperty<Orientation> OrientationProperty = AvaloniaProperty.Register<Group, Orientation>(
"Orientation", Orientation.Vertical);
public Orientation Orientation
{
get => GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
public static readonly StyledProperty<double> GapProperty = AvaloniaProperty.Register<Group, double>(
"Gap", 10);
public double Gap
{
get => GetValue(GapProperty);
set => SetValue(GapProperty, value);
}
protected override Size MeasureOverride(Size availableSize)
{
return GroupUtils.Measure(availableSize, Children, Orientation, Gap);
}
protected override Size ArrangeOverride(Size finalSize)
{
return GroupUtils.ArrangeGroup(finalSize, Children, Orientation, Gap);
}
}
public class VGroup : Panel
{
public static readonly StyledProperty<double> GapProperty = AvaloniaProperty.Register<Group, double>(
"Gap", 10);
public double Gap
{
get => GetValue(GapProperty);
set => SetValue(GapProperty, value);
}
protected override Size MeasureOverride(Size availableSize)
{
return GroupUtils.Measure(availableSize, Children, Orientation.Vertical, Gap);
}
protected override Size ArrangeOverride(Size finalSize)
{
return GroupUtils.ArrangeGroup(finalSize, Children, Orientation.Vertical, Gap);
}
}
public class HGroup : Panel
{
public static readonly StyledProperty<double> GapProperty = AvaloniaProperty.Register<Group, double>(
"Gap", 10);
public double Gap
{
get => GetValue(GapProperty);
set => SetValue(GapProperty, value);
}
protected override Size MeasureOverride(Size availableSize)
{
return GroupUtils.Measure(availableSize, Children, Orientation.Horizontal, Gap);
}
protected override Size ArrangeOverride(Size finalSize)
{
return GroupUtils.ArrangeGroup(finalSize, Children, Orientation.Horizontal, Gap);
}
}
public static class GroupUtils
{
public static Size Measure(Size availableSize, Controls children, Orientation orientation, double gap)
{
Size layoutSlotSize = availableSize;
Size desiredSize = new Size();
bool hasVisibleChild = false;
//In order to handle percentwidth and percentheight scenario's we first have to measure all the children to determine their constrained measurement
//then depending on the orientation we factor in the left over space available and split that up via the percentages and orientation
//we use the measure with the true override to force the child to take our supplied size instead of it's default constrained size
var percentHeightChildrenMap = new Dictionary<Layoutable, double>();
var percentWidthChildrenMap = new Dictionary<Layoutable, double>();
//loop through all children and determine constrained size and check if percent height is set
for (int i = 0, count = children.Count; i < count; ++i)
{
// Get next child.
var child = children[i];
if (child == null) { continue; }
bool isVisible = child.IsVisible;
if (isVisible && !hasVisibleChild)
{
hasVisibleChild = true;
}
if (!double.IsNaN(child.PercentHeight))
{
percentHeightChildrenMap[child] = child.PercentHeight;
}
if (!double.IsNaN(child.PercentWidth))
{
percentWidthChildrenMap[child] = child.PercentWidth;
}
// Measure the child.
child.Measure(layoutSlotSize);
var childDesiredSize = child.DesiredSize;
if (orientation == Orientation.Vertical)
{
//in vertical mode, our width is the max width of the children
desiredSize = desiredSize.WithWidth(Math.Max(desiredSize.Width, childDesiredSize.Width));
//our height is the combine height of the children
desiredSize = desiredSize.WithHeight(desiredSize.Height + (isVisible ? gap : 0) + childDesiredSize.Height);
}
else
{
//in horizontal mode, our height is the max height of the children
desiredSize = desiredSize.WithHeight(Math.Max(desiredSize.Height, childDesiredSize.Height));
//our height is the combine width of the children
desiredSize = desiredSize.WithWidth(desiredSize.Width + (isVisible ? gap : 0) + childDesiredSize.Width);
}
}
if (orientation == Orientation.Vertical)
{
//Handle percent width
foreach (var child in children)
{
if (!double.IsNaN(child.PercentWidth))
{
child.InvalidateMeasure();
child.Measure(child.DesiredSize.WithWidth(child.PercentWidth * 0.01 * availableSize.Width), true);
desiredSize = desiredSize.WithWidth(Math.Max(desiredSize.Width, child.DesiredSize.Width));
}
}
//if we have dont have a visible child then set to 0, otherwise remove the last added gap
desiredSize = desiredSize.WithHeight(desiredSize.Height - (hasVisibleChild ? gap : 0));
if (hasVisibleChild && percentHeightChildrenMap.Count > 0)
{
//for those with percent height set, combine the percent heights together and if above 100, find the scale factor
var totalPercentHeight = percentHeightChildrenMap.Sum(v => v.Value);
totalPercentHeight = totalPercentHeight <= 0 ? 1 : totalPercentHeight;
var scaleRatio = 1 / (totalPercentHeight / 100);
//the available size leftover after the non-percent height children is now used to calculate the percentheight children sizes
var availableHeight = availableSize.Height - desiredSize.Height;
Debug.WriteLine($"Remapping %Height Children, availableHeight: {availableHeight}, scaleRatio: {scaleRatio}" );
foreach (var child in percentHeightChildrenMap.Keys)
{
var originalHeight = child.DesiredSize.Height;
var percentHeight = percentHeightChildrenMap[child];
var heightIncrease = availableHeight * percentHeight * scaleRatio * 0.01;
var recalculatedHeight = child.DesiredSize.Height + heightIncrease;
child.InvalidateMeasure();
child.Measure(child.DesiredSize.WithHeight(recalculatedHeight), true);
desiredSize = desiredSize.WithHeight(desiredSize.Height + child.DesiredSize.Height - originalHeight);
Debug.WriteLine($"$Found Child Height %:{percentHeight}, Original Height: {originalHeight}, New: {recalculatedHeight}" );
}
}
}
else
{
//Handle percent height
foreach (var child in children)
{
if (!double.IsNaN(child.PercentHeight))
{
child.InvalidateMeasure();
child.Measure(child.DesiredSize.WithHeight(child.PercentHeight * 0.01 * availableSize.Height), true);
desiredSize = desiredSize.WithHeight(Math.Max(desiredSize.Height, child.DesiredSize.Height));
}
}
//if we have dont have a visible child then set to 0, otherwise remove the last added gap
desiredSize = desiredSize.WithWidth(desiredSize.Width - (hasVisibleChild ? gap : 0));
if (hasVisibleChild && percentWidthChildrenMap.Count > 0)
{
//for those with percent Width set, combine the percent Widths together and if above 100, find the scale factor
var totalPercentWidth = percentWidthChildrenMap.Sum(v => v.Value);
totalPercentWidth = totalPercentWidth <= 0 ? 1 : totalPercentWidth;
var scaleRatio = 1 / (totalPercentWidth / 100);
//the available size leftover after the non-percent height children is now used to calculate the percentheight children sizes
var availableWidth = availableSize.Width - desiredSize.Width;
Debug.WriteLine($"Remapping %Width Children, availableWidth: {availableWidth}, scaleRatio: {scaleRatio}" );
foreach (var child in percentWidthChildrenMap.Keys)
{
var originalWidth = child.DesiredSize.Width;
var percentWidth = percentWidthChildrenMap[child];
var widthIncrease = availableWidth * percentWidth * scaleRatio * 0.01;
var recalculatedWidth = child.DesiredSize.Width + widthIncrease;
child.InvalidateMeasure();
child.Measure(child.DesiredSize.WithWidth(recalculatedWidth), true);
desiredSize = desiredSize.WithWidth(desiredSize.Width + child.DesiredSize.Width - originalWidth);
Debug.WriteLine($"$Found Child Width %:{percentWidth}, Original Width: {originalWidth}, New: {recalculatedWidth}" );
}
}
}
return desiredSize;
}
public static Size ArrangeGroup(Size finalSize, Controls children, Orientation orientation, double gap)
{
bool fHorizontal = (orientation == Orientation.Horizontal);
Rect rcChild = new Rect(finalSize);
double previousChildSize = 0.0;
var spacing = gap;
//
// Arrange and Position Children.
//
for (int i = 0, count = children.Count; i < count; ++i)
{
var child = children[i];
if (child == null || !child.IsVisible)
{
continue;
}
if (fHorizontal)
{
rcChild = rcChild.WithX(rcChild.X + previousChildSize);
previousChildSize = child.DesiredSize.Width;
rcChild = rcChild.WithWidth(previousChildSize);
rcChild = rcChild.WithHeight(child.DesiredSize.Height);
previousChildSize += spacing;
}
else
{
rcChild = rcChild.WithY(rcChild.Y + previousChildSize);
previousChildSize = child.DesiredSize.Height;
rcChild = rcChild.WithHeight(previousChildSize);
rcChild = rcChild.WithWidth(child.DesiredSize.Width);
previousChildSize += spacing;
}
child.Arrange(rcChild);
}
return finalSize;
}
}
Finally I had to make a change in the avalonia source class Layoutable
adding
public static readonly StyledProperty<double> PercentWidthProperty = AvaloniaProperty.Register<Layoutable, double>(
"PercentWidth", Double.NaN);
public static readonly StyledProperty<double> PercentHeightProperty = AvaloniaProperty.Register<Layoutable, double>(
"PercentHeight", Double.NaN);
public double PercentHeight
{
get => GetValue(PercentHeightProperty);
set => SetValue(PercentHeightProperty, value);
}
public double PercentWidth
{
get => GetValue(PercentWidthProperty);
set => SetValue(PercentWidthProperty, value);
}
Registering the properties in the constructor for layoutable such as
static Layoutable()
{
AffectsMeasure<Layoutable>(
WidthProperty,
HeightProperty,
MinWidthProperty,
MaxWidthProperty,
MinHeightProperty,
MaxHeightProperty,
MarginProperty,
**PercentHeightProperty,
PercentWidthProperty,**
HorizontalAlignmentProperty,
VerticalAlignmentProperty);
}
and modifying the measure method to accept a boolean 2nd parameter that tells the measure to use all available space and then uses the percentage calculation:
public void Measure(Size availableSize, bool useAvailable = false)
{
if (double.IsNaN(availableSize.Width) || double.IsNaN(availableSize.Height))
{
throw new InvalidOperationException("Cannot call Measure using a size with NaN values.");
}
if (!IsMeasureValid || _previousMeasure != availableSize)
{
var previousDesiredSize = DesiredSize;
var desiredSize = default(Size);
IsMeasureValid = true;
try
{
_measuring = true;
desiredSize = MeasureCore(availableSize);
//used in percentwidth height layout system
if (useAvailable == true)
{
desiredSize = desiredSize.WithHeight(Math.Max(availableSize.Height, desiredSize.Height))
.WithWidth(Math.Max(availableSize.Width, desiredSize.Width));
}
}
finally
{
_measuring = false;
}
if (IsInvalidSize(desiredSize))
{
throw new InvalidOperationException("Invalid size returned for Measure.");
}
DesiredSize = desiredSize;
_previousMeasure = availableSize;
Logger.TryGet(LogEventLevel.Verbose, LogArea.Layout)?.Log(this, "Measure requested {DesiredSize}", DesiredSize);
if (DesiredSize != previousDesiredSize)
{
this.GetVisualParent<Layoutable>()?.ChildDesiredSizeChanged(this);
}
}
}
I'd suggest reading the documentation when picking up a new UI tech. The worst thing you can do is try to bend a new technology to the way another unrelated technology works.
Particularly when what you need already exists.
50% / 50% columns ...
<Grid ColumnDefinitions="1*, 1*">
<Border Grid.Column="0" Background="Red" />
<Border Grid.Column="1" Background="Blue" />
</Grid>
25% / 75%
<Grid ColumnDefinitions="1*, 3*">
<Border Grid.Column="0" Background="Red" />
<Border Grid.Column="1" Background="Blue" />
</Grid>
You typically don't set heights and widths on controls. You define the space they have on the UI and allow them to adapt. Some controls might have a default height in a style that's applied globally.
Try to think in XAML terms when using XAML and Adobe terms when using Adobe. Mixing the two will self-inflict a lot of pain.
I'd advise anyone else finding this question to not use this percentage approach.

Creating Grid at Runtime through code in Xamarin

I have code as per attached that builds a Xamrin Grid virtually in code. The problem that I have not been able to resolve is Events that will be triggered when entering data into a cell.
Here is the output:
Output 1
Output 2
The purpose of this post is to create an event that can be invoked to take action when data is entered into any of the cells created at runtime in the grid.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="VarGridSample.MainPage">
<ScrollView>
<StackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Variable Grid"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="I am attempting to build a Grid at runtime to allow for variable rows and columns. The Grid has been initially defined in the Content Page. The Grid is built in the code based on the number of rows and columns requested. Entry controls are built for each cell. The problem is how to build the Events for each cell...ANY IDEAS?"
FontSize="18"
HorizontalOptions="Center" />
<Entry x:Name="Rows"
Text="{Binding Rows }"
HorizontalOptions="Center" Placeholder="Enter Total Rows" WidthRequest="150" />
<Entry x:Name="Cols"
Text="{Binding Rows }"
HorizontalOptions="Center" Placeholder="Enter Total Columns" WidthRequest="150" />
<Button x:Name="BuildGrid"
Text="Build Grid" HorizontalOptions="Center" Clicked="BuildGrid_Clicked" />
<Grid x:Name="VarGrid">
</Grid>
</StackLayout>
</ScrollView>
</ContentPage>
Below is the C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace VarGridSample
{
public partial class MainPage : ContentPage
{
private int rows { get; set; }
private int cols { get; set; }
private int cellcntr = 0;
public MainPage()
{
InitializeComponent();
}
private void BuildGrid_Clicked(object sender, EventArgs e)
{
rows = Int32.Parse(Rows.Text);
cols = Int32.Parse(Cols.Text);
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
VarGrid.RowDefinitions.Add(new RowDefinition());
}
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int colIndex = 0; colIndex < cols; colIndex++)
{
var entry = new Entry
{
Text = "cell" + (cellcntr).ToString(),
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
VarGrid.Children.Add(entry, colIndex, rowIndex);
cellcntr++;
}
}
}
}
}
You can add this to MainPage.xaml.cs:
private void BuildGrid_Clicked(object sender, EventArgs e)
{
rows = Int32.Parse(Rows.Text);
cols = Int32.Parse(Cols.Text);
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
VarGrid.RowDefinitions.Add(new RowDefinition());
}
for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
for (int colIndex = 0; colIndex < cols; colIndex++)
{
var entry = new Entry
{
Text = "cell" + (cellcntr).ToString(),
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
};
//add the event
entry.TextChanged += Entry_TextChanged;
VarGrid.Children.Add(entry, colIndex, rowIndex);
cellcntr++;
}
}
}
private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
Console.WriteLine(e.OldTextValue);
var entry = (Entry)sender;
var parent= (Grid)entry.Parent;
Console.WriteLine(parent.Children.Count);
}

Insert symbol in the Value xaml

<controls:RadialProgressBar Margin="10,0,0,0" x:Name="RadialProgressBarControl"
Value="0"
Minimum="0"
Maximum="180"
Thickness="0"
Outline="#FFD6DCDA"
Foreground="#FF9646E6"
Width="300"
Height="300" FontFamily="Palatino Linotype">
</controls:RadialProgressBar>
<TextBlock Canvas.ZIndex="100" Margin="10,0,0,0" FontFamily="Palatino Linotype" FontSize="55" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF786DDB" Text="{x:Bind RadialProgressBarControl.Value, Mode=OneWay}">
while (RadialProgressBarControl.Value < 180)
{
RadialProgressBarControl.Value += 1;
RadialProgressBarControl.Thickness += 0.15;
await Task.Delay(5);
}
I have 2 problem
is how can I insert the "%" to the ?
I want my circle "path" change the color while load the value from one to 180
is how can I insert the "%" to the ?
If you want to insert %to your TextBlock, you could create a TextConverter inherit IValueConverter. And use it to format text like the follow.
internal class TextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return string.Format("{0} %", value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Usage
<Page.Resources>
<local:TextConverter x:Key="MyConverter" />
</Page.Resources>
<TextBlock Text="{x:Bind RadialProgressBarControl.Value, Mode=OneWay, Converter={StaticResource MyConverter}}"></TextBlock>
I want my circle "path" change the color while load the value from one to 180
You could set the foreground of RadialProgressBarControl in the code behind with SolidColorBrush dynamically.
Color Tem = new Color();
while (RadialProgressBarControl.Value < 180)
{
RadialProgressBarControl.Value += 1;
RadialProgressBarControl.Thickness += 0.15;
Tem.A = 255;
Tem.R += 1;
Tem.B += 1;
Tem.G += 1;
RadialProgressBarControl.Foreground = new SolidColorBrush(Tem);
await Task.Delay(5);
}

Rich TextBlock in ListViewItem Text Wrapping

I have a RichTextBlock in a ListViewItem in a ListView. I can't for the life of me findout why the text wrapping on the RichTextBlock won't apply.
XAML:
<ScrollViewer x:Name="MessagesScroller" HorizontalScrollMode="Disabled">
<ListView x:Name="Messages" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</ScrollViewer>
C#
ListViewItem listviewitem = new ListViewItem();
listviewitem.HorizontalContentAlignment = HorizontalAlignment.Stretch;
listviewitem.VerticalAlignment = VerticalAlignment.Stretch;
listviewitem.Tag = message.Id;
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
stack.VerticalAlignment = VerticalAlignment.Stretch;
Image Avatar = new Image();
Avatar.Height = 50;
Avatar.VerticalAlignment = VerticalAlignment.Top;
Avatar.Source = new BitmapImage(new Uri("https://cdn.discordapp.com/avatars/" + message.User.Id + "/" + message.User.Avatar + ".jpg"));
stack.Children.Add(Avatar);
StackPanel innerstack = new StackPanel();
innerstack.VerticalAlignment = VerticalAlignment.Stretch;
StackPanel MsgData = new StackPanel();
MsgData.Orientation = Orientation.Horizontal;
#region RichTextBlock
RichTextBlock user = new RichTextBlock();
user.TextWrapping = TextWrapping.WrapWholeWords;
Paragraph userPara = new Paragraph();
Run run1 = new Run();
run1.Text = message.User.Username;
userPara.Inlines.Add(run1);
user.Blocks.Add(userPara);
#endregion
MsgData.Children.Add(user);
#region RichTextBlock
RichTextBlock timestamp = new RichTextBlock();
Paragraph timePara = new Paragraph();
Run run2 = new Run();
run2.Text = message.Timestamp.Month.ToString() + "/" + message.Timestamp.Day + " at " + message.Timestamp.Hour.ToString() + ":";
if (message.Timestamp.Minute < 9)
{
run2.Text += "0";
}
run2.Text += message.Timestamp.Minute.ToString();
timestamp.Foreground = GetSolidColorBrush("#FF333333");
timePara.Inlines.Add(run2);
timestamp.Blocks.Add(timePara);
timestamp.Margin = new Thickness(5, 0, 0, 0);
#endregion
MsgData.Children.Add(timestamp);
innerstack.Children.Add(MsgData);
#region RichTextBlock
RichTextBlock txtblock = new RichTextBlock();
txtblock.TextWrapping = TextWrapping.WrapWholeWords;
Paragraph txtPara = new Paragraph();
Run run3 = new Run();
run3.Text = message.Content;
txtPara.Inlines.Add(run3);
txtblock.Blocks.Add(txtPara);
foreach (SharedModels.Embed embed in message.Embeds)
{
Paragraph paragraph = new Paragraph();
if (embed.title != null)
{
Run title = new Run();
title.Text = embed.title + "\n";
paragraph.Inlines.Add(title);
}
if (embed.Description != null)
{
Run desc = new Run();
desc.Text = embed.Description + "\n";
paragraph.Inlines.Add(desc);
}
if (embed.Thumbnail.Url != null)
{
InlineUIContainer container = new InlineUIContainer();
BitmapImage bi = new BitmapImage(new Uri(embed.Thumbnail.ProxyUrl));
Image image = new Image();
image.Height = 300;
image.Source = bi;
container.Child = image;
paragraph.Inlines.Add(container);
}
txtblock.Blocks.Add(paragraph);
}
foreach (SharedModels.Attachment attach in message.Attachments)
{
Paragraph paragraph = new Paragraph();
Run run = new Run();
run.Text = attach.Filename;
BitmapImage bi = new BitmapImage(new Uri(attach.Url));
Image image = new Image();
image.Height = 300;
image.Source = bi;
InlineUIContainer container = new InlineUIContainer();
container.Child = image;
paragraph.Inlines.Add(run);
paragraph.Inlines.Add(container);
txtblock.Blocks.Add(paragraph);
}
#endregion
innerstack.Children.Add(txtblock);
stack.Children.Add(innerstack);
listviewitem.Content = stack;
#region Flyout
Flyout flyout = new Flyout();
StackPanel flyoutcontent = new StackPanel();
flyoutcontent.Margin = new Thickness(-10);
/*Button AddRection = new Button()
{
Content = "Add Reaction",
HorizontalAlignment = HorizontalAlignment.Stretch
};
flyoutcontent.Children.Add(AddRection);
Button Pin = new Button()
{
Content = "Pin",
HorizontalAlignment = HorizontalAlignment.Stretch
};
flyoutcontent.Children.Add(Pin);*/
/*Button Edit = new Button()
{
Content = "Edit",
HorizontalAlignment = HorizontalAlignment.Stretch,
Tag = message.Id
};
Edit.Click += EditMessage;
flyoutcontent.Children.Add(Edit);*/
Button Delete = new Button()
{
Content = "Delete",
HorizontalAlignment = HorizontalAlignment.Stretch,
Tag = message.Id
};
Delete.Click += DeleteThisMessage;
flyoutcontent.Children.Add(Delete);
flyout.Content = flyoutcontent;
listviewitem.ContextFlyout = flyout;
listviewitem.RightTapped += OpenRightTapFlyout;
listviewitem.Holding += OpenHoldingFlyout;
#endregion
Messages.Items.Add(listviewitem);
I have uprooted my entire program to use StackPanels instead but that didn't help
You used StackPanel for your layout and set the Orientation property to Horizontal for the StackPanel. StackPanel is size to its children. So it seems like the reason is that you don't set the Width property for the RichTextBlock. The width of RichTextBlock size to its content, StackPanelsize toRichTextBlock`, it may not wrap.
You may have several ways to resolve this:
Set width property for the RichTextBlock.
RichTextBlock txtblock = new RichTextBlock();
txtblock.Width = 300;
txtblock.TextWrapping = TextWrapping.WrapWholeWords;
Remove the orientation property for the StackPanel if you don't need such a layout.
//stack.Orientation = Orientation.Horizontal;
Or you may use a Grid instead. Grid may better help you define the layout. You may define the image and RichTextBlock layout as followings.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="Assets/caffe1.jpg" Height="100" Grid.Row="0" Grid.Column="0"></Image>
<RichTextBlock TextWrapping="WrapWholeWords" Grid.Row="0" Grid.Column="1">
<Paragraph>
<Run FontStyle="Italic">sample text todemonstrate some properties.demonstrate some properties.demonstrate some properties.demonstrate some properties.
demonstrate some properties.demonstrate some properties.demonstrate some properties.demonstrate some properties.demonstrate some properties.
</Run>
</Paragraph>
</RichTextBlock>
</Grid>

Implementing DragStarted DragDelta events in windows 8 / WinRT

How I can attach DragStarted DragDelta events to a grid in windows 8 / WinRT. I did the Same in windows phone with GestureService.GetGestureListener() method. I tried to replace the code with ManipulationStarted & ManipulationDelta events in windows 8. But the result is not same. In windows phone for a single drag it enters DragDelta events 2 or more times. But in the other hand in windows 8, in ManupulationDelta event it only fires once for the similar Drag operation.
Yeah, I think I know what you want.
Let's say you have some XAML like this:
<Grid Margin="50">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Rectangle Fill="Blue" x:Name="MyRect" />
</Grid>
You want to move that rectangle around the Grid by dragging it.
Just use this code:
public MainPage()
{
this.InitializeComponent();
MyRect.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
MyRect.ManipulationDelta += Rectangle_ManipulationDelta;
MyRect.ManipulationCompleted += Rectangle_ManipulationCompleted;
}
private void Rectangle_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var _Rectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
var _Transform = (_Rectangle.RenderTransform = (_Rectangle.RenderTransform as TranslateTransform) ?? new TranslateTransform()) as TranslateTransform;
_Transform.X += e.Delta.Translation.X;
_Transform.Y += e.Delta.Translation.Y;
}
private void Rectangle_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
var _Rectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
_Rectangle.RenderTransform = null;
var _Column = System.Convert.ToInt16(_Rectangle.GetValue(Grid.ColumnProperty));
if (_Column <= 0 && e.Cumulative.Translation.X > _Rectangle.RenderSize.Width * .5)
_Rectangle.SetValue(Grid.ColumnProperty, 1);
else if (_Column == 1 && e.Cumulative.Translation.X < _Rectangle.RenderSize.Width * -.5)
_Rectangle.SetValue(Grid.ColumnProperty, 0);
var _Row = System.Convert.ToInt16(_Rectangle.GetValue(Grid.RowProperty));
if (_Row <= 0 && e.Cumulative.Translation.Y > _Rectangle.RenderSize.Height * .5)
_Rectangle.SetValue(Grid.RowProperty, 1);
else if (_Row == 1 && e.Cumulative.Translation.Y < _Rectangle.RenderSize.Height * -.5)
_Rectangle.SetValue(Grid.RowProperty, 0);
}
For this:
Hope I'm close!
Best of luck!