about wrap text in ContentDialogResult in winphone 8.1 RT - c#

I have long string and show it in ContentDialogResult as
var dlg = new ContentDialog()
{
Title = sTitle,
Content = "This is very long string and i want wrap it but it only appear in 1 line on content of ContentDialogResult. Please help me!",
PrimaryButtonText = sTextBtnMain,
SecondaryButtonText = sTextBtnSub
};
but it only show on 1 line and no wrap. How i can wrap it without custom xaml for ContentDialogResult.
Thanks for all support!

ContentDialog accepts any object as Title and Content property. So, create TextBlock, add TextWrapping property and pass it to Content instead of string object. It will work.
TextBlock txtBlock = new TextBlock();
txtBlock.Text = "This is very long string and i want wrap it but it only appear in 1 line on content of ContentDialogResult. Please help me!";
txtBlock.TextWrapping = TextWrapping.Wrap;
ContentDialog dialog = new ContentDialog()
{
Title = sTitle,
Content = txtBlock,
PrimaryButtonText = sTextBtnMain,
SecondaryButtonText = sTextBtnSub
};

Related

Change Visual State Pressed Button Background UWP

I know how to do this in XAML, but I want to change the colors in a ContentDialog.
I can change the background and forground color, but I would also like to change the button colors when in a specific state ("Pressed", "PointOver", ...).
var contentDialog = new ContentDialog
{
Content = "Test",
Title = "Test",
PrimaryButtonText = "OK"
};
var buttonStyle = new Style(typeof(Button));
buttonStyle.Setters.Add(new Setter(Control.BackgroundProperty, Colors.Yellow));
buttonStyle.Setters.Add(new Setter(Control.ForegroundProperty, Colors.White));
buttonStyle.Setters.Add(new Setter(Windows.UI.Xaml.Controls.Primitives.ButtonBase.IsPressedProperty, Colors.Green));
buttonStyle.Setters.Add(new Setter(VisualState...))
contentDialog.PrimaryButtonStyle = buttonStyle;
await contentDialog.ShowAsync();
How can I do this in code?
Override the ButtonBackgroundPressed theme resource:
contentDialog.Resources["ButtonBackgroundPressed"] = new SolidColorBrush(Colors.Red);
Or define a completely custom ControlTemplate:
const string Xaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"Button\">" +
"...</ControlTemplate>";
var buttonStyle = new Style(typeof(Button));
buttonStyle.Setters.Add(new Setter(Control.TemplateProperty, XamlReader.Load(Xaml) as ControlTemplate));
contentDialog.PrimaryButtonStyle = buttonStyle;

how to add styles for content dialog title and button using c# for UWP app

ContentDialog Dialog = new ContentDialog();
private async void DisplaySessionDialog()
{
var bStyle = new Style(typeof(Button));
bStyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush(Colors.White)));
bStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Colors.Maroon)));
bStyle.Setters.Add(new Setter(Button.HorizontalContentAlignmentProperty, HorizontalAlignment.Center));
bStyle.Setters.Add(new Setter(Button.VerticalContentAlignmentProperty, VerticalAlignment.Center));
bStyle.Setters.Add(new Setter(Button.CornerRadiusProperty, new CornerRadius(15)));
bStyle.Setters.Add(new Setter(Button.PaddingProperty, new Thickness(5)));
bStyle.Setters.Add(new Setter(Button.MarginProperty, HorizontalAlignment.Center));
bStyle.Setters.Add(new Setter(Button.FontSizeProperty, 18));
Dialog.Title = val.SessionExpireTitle;
Dialog.Content = val.SessionExpireContent;
Dialog.PrimaryButtonText = val.SessionExpireButtonLabel;
Dialog.PrimaryButtonStyle = bStyle;
Dialog.FontSize = 18;
await Dialog.ShowAsync();
}
The above code is working fine. However, I want to alignment button as a centre and title alignment centre, content also from left. I tried more ways and search a lot of things I did not find any solutions . Please help me, is there any way to align title in c# for UWP app

Retreive stackpanel children from GridViewItem (UWP)

I'm wondering how I can get the contents of a stackpanel which has been added to a GridViewItem (YouTube data API, the stackpanel contains a bitmap image for a thumbnail, the title of the video and a hidden TextBlock with the video ID for reference).
To clarify, I need to be able to obtain the contents of the StackPanel that is contained in the GridViewItem (such as the string of that hidden TextBox) so I can use the data to display the correct video.
Here's the code I use to create the stackpanel.
public async Task SearchByKeyword(string searchTerm)
{
GeneralFunctions gf = new GeneralFunctions();
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = gf.YouTubeAPIKey,
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchTerm; // Replace with your search term.
searchListRequest.MaxResults = 50;
searchListRequest.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.Strict;
// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = await searchListRequest.ExecuteAsync();
List<string> videos = new List<string>();
List<string> channels = new List<string>();
List<string> playlists = new List<string>();
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach (var searchResult in searchListResponse.Items)
{
switch (searchResult.Id.Kind)
{
case "youtube#video":
// Create a new StackPanel to insert as a ListViewItem
StackPanel sPanel = new StackPanel();
sPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
sPanel.VerticalAlignment = VerticalAlignment.Stretch;
sPanel.Orientation = Orientation.Vertical;
sPanel.Width = 160;
sPanel.Height = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb1 = new TextBlock();
tb1.Text = searchResult.Snippet.Title;
tb1.TextWrapping = TextWrapping.WrapWholeWords;
tb1.Width = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb2 = new TextBlock();
tb2.Text = searchResult.Snippet.ChannelId;
tb2.TextWrapping = TextWrapping.WrapWholeWords;
tb2.Width = 160;
// Create new StackPanel child element for a 120x120 thumbnail of the videos from the search results
Image image = new Image();
image.Source = new BitmapImage(new Uri(searchResult.Snippet.Thumbnails.Default__.Url, UriKind.Absolute));
// Add a "hidden" child element to each stackpanel to hold the video identity
TextBlock h1 = new TextBlock();
h1.Text = searchResult.Id.VideoId.ToString();
h1.Visibility = Visibility.Collapsed;
h1.TextWrapping = TextWrapping.WrapWholeWords;
sPanel.Children.Add(image);
sPanel.Children.Add(tb1);
sPanel.Children.Add(tb2);
sPanel.Children.Add(h1);
SearchResults.Items.Add(sPanel);
break;
case "youtube#channel":
//SearchResults.Items.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
break;
case "youtube#playlist":
//playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
break;
}
}
//Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
//Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
//Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
}
Any help is appreciated.
Thanks
I guess I should never code tired right? The answer to my own question is actually very very simple
if (SearchResults.SelectedItems.Count > 0)
{
StackPanel sp = (StackPanel)SearchResults.SelectedItem;
TextBlock tb1 = (TextBlock)sp.Children[1];
TextBlock tb2 = (TextBlock)sp.Children[2];
TextBlock hf1 = (TextBlock)sp.Children[3];
}

Showing Font Selection Dialog in GTK#

In MY gtk# Application im trying to show the font selection dialog.Im trying to use the following code,but the FontSelectionDialog constructor need some arguments also does the control execution wait for a font to be selected to set the string font
Can someone guide me?
Gtk.FontSelectionDialog fs = new FontSelectionDialog()
fs.Show ();
font=fs.FontName;
Updated according to additional question
This should help:
FontSelectionDialog dialog = null;
try {
dialog = new FontSelectionDialog("Choose a font");
dialog.Run ();
var name = dialog.FontName;
var pattern = #"^(?<fontName>.*)\s(?<fontSize>\d+(?:\.\d+)?)$";
var regex = new Regex(pattern);
var match = regex.Match(name);
if(match.Success)
{
var fontName = match.Groups["fontName"].Value;
var fontSize = float.Parse(match.Groups["fontSize"].Value);
var font = new System.Drawing.Font(fontName, fontSize);
}
} finally {
if (dialog != null)
dialog.Destroy ();
}

How to retrieve the name of a Panorama-Item at runtime?

I am programmatically adding items to my Panorama Control called PanoramaCC.
//function to create the panorama items in our view
private void showPanorama(string panoramaName)
{
//create the panorama item and define it
PanoramaItem genItem = new PanoramaItem();
genItem.Height = 265;
genItem.Width = 440;
genItem.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(PanoramaItem_Tap);
genItem.Name = panoramaName;
//create the stackpanel for the panoramaitem
StackPanel genStack = new StackPanel();
genStack.Orientation = System.Windows.Controls.Orientation.Horizontal;
//margin to be done
genStack.Margin = new Thickness(0, -20, 0, 20);
//load the image
Image genImg = new Image();
genImg.Height = 220;
genImg.Width = 400;
genImg.Stretch = System.Windows.Media.Stretch.Fill;
genImg.Margin = new Thickness(20, 5, 20, 5);
string path = "Assets/AppGraphics/CreditCards/" + panoramaName.ToString() + "Front.png";
Uri uriR = new Uri(path, UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uriR);
genImg.Source = imgSource;
//add image into stackpanel
genStack.Children.Add(genImg);
//add stackpanel to the panoramaitem
genItem.Content = genStack;
//add the panoramaitem to the panoramaview
this.PanoramaCC.Items.Add(genItem);
}
The issue I have is that during runtime I want to retrieve the name of the panoramaItem I am currently looking at and do something with it. I've managed to retrieve the name through the tap event for navigation purposes, string name = ((PanoramaItem)sender).Name; but this is a diffrent scenario. I want to retrieve the name and then delete the item with the corresponding name. Pressing a button should delete the currently selected panoramaItem, is what I'm trying to achieve.
You can get the current PanoramaItem by using the SelectedItem property. You don't need to get the name to delete it.
PanoramaItem currentItem = myPanorama.SelectedItem as PanoramaItem;
if(currentItem != null)
{
//if you want the name for other reasons
string name = currentItem.Name;
//Items returns an ItemsCollection object
myPanorama.Items.Remove(currentItem);
}

Categories

Resources