How can i convert this UI into code behind wpf
<TextBlock x:Name="tblImgCorrectAnswer">
<Span>Hello</Span>
<Span Style="{DynamicResource FontMSMincho}">て</Span>
<InlineUIContainer BaselineAlignment="Center">
<TextBlock>
<Image Source="Images/icon.ico" Width="40" Height="40"/>
</TextBlock>
</InlineUIContainer>
</TextBlock>
I cann't anyway to add item control into textblock control or TextBlock control into InlineUIContainer control by using code-behind.
Thanhks.
The result like This
Code Behind
var textBlock = new TextBlock();
var inlineHello = new Span();
inlineHello.Inlines.Add("Hello");
var inlineJSighn = new Span();
inlineJSighn.Inlines.Add(" JSighn");
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/Images/Koala.jpg", UriKind.Absolute);
logo.EndInit();
var inlineUiContainer = new InlineUIContainer(new Image { Source = logo });
inlineUiContainer.BaselineAlignment = BaselineAlignment.Center;
textBlock.Inlines.Add(inlineHello);
textBlock.Inlines.Add(inlineJSighn);
textBlock.Inlines.Add(inlineUiContainer);
LayoutRoot.Children.Add(textBlock);
Xaml
<Window x:Class="AddChildrenToGrinInCode.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot"/></Window>
Related
I want a colorful background. My code not work. Version OxyPlotWpf: 1.0.0-unstable2100. Date published: Tuesday, April 12, 2016 (4/12/2016)
Can not imagine what else you can write in detail, my questions not passed since the system thinks there is not enough detail, so I added the text (sorry)
What i have
What i want
My GrpahPhone.png
<Window x:Class="OxiPlotTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OxiPlotTest"
xmlns:oxy="clr-namespace:OxyPlot.Wpf;assembly=OxyPlot.Wpf"
xmlns:oxy2="http://oxyplot.codeplex.com"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="750"
Background="#333333">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<oxy:Plot Grid.Row="0" x:Name="oxyPlot2" Title="" Margin="10" Background="Transparent" PlotAreaBorderThickness="0,0,0,1" PlotAreaBorderColor="#CCCCCC">
<oxy:Plot.Axes>
<oxy:LinearAxis Position="Left" Minimum="0" Maximum="100" MajorGridlineColor="#333333" MajorGridlineStyle="Solid" MajorGridlineThickness="1" MajorStep="25" MinorStep="25"
TickStyle="None" TextColor="#CCCCCC" FontSize="16" FontWeight="Bold" AxisDistance="5"/>
<oxy:LinearAxis Position="Bottom" Minimum="0" Maximum="5" MajorGridlineStyle="None" MajorStep="1" MinorStep="1"
TextColor="#CCCCCC" FontSize="16" FontWeight="Bold" TicklineColor="#CCCCCC"/>
</oxy:Plot.Axes>
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}" Color="AliceBlue" StrokeThickness="4"/>
</oxy:Plot.Series>
</oxy:Plot>
</Grid>
public partial class MainWindow : Window
{
public IList<DataPoint> Points { get; private set; }
public MainWindow()
{
InitializeComponent();
Points = new List<DataPoint>
{
new DataPoint(0, 5),
new DataPoint(1, 40),
new DataPoint(2, 55),
new DataPoint(3, 80),
new DataPoint(4, 90),
new DataPoint(5, 50),
};
DataContext = this;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("pack://application:,,,/OxiPlotTest;component/Resources/GraphFon.png", UriKind.Absolute);
bitmapImage.EndInit();
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)bitmapImage));
var stream = new System.IO.MemoryStream();
encoder.Save(stream);
stream.Flush();
var image = new System.Drawing.Bitmap(stream);
var bitmap = new System.Drawing.Bitmap(image);
var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
bitmap.Dispose();
var brush = new ImageBrush(bitmapSource);
oxyPlot2.PlotAreaBackground = brush;
}
}
I made 5 different List Points by color and added LineSeries
I want to take a given BitmapImage and use its grayscale/black-and-white representation as an opacity mask on a DrawingContext.
I got far enough to do the color conversion, so I ended up with the follwing state of a demo application:
Method on ViewModel:
public void Demo()
{
var width = 400;
var height = 400;
var target = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
var target2 = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
var drawingVisual = new DrawingVisual();
var drawingVisual2 = new DrawingVisual();
using (var drawingContext = drawingVisual.RenderOpen())
{
var bitmapImage = new BitmapImage(new Uri(#"pack://application:,,,/WarningFilled.png"));
using (var drawingContext2 = drawingVisual2.RenderOpen())
{
drawingContext2.DrawImage(bitmapImage, new Rect(0, 0, width, height));
}
target2.Render(drawingVisual2);
var mask = target2.ToGrayscale();
this.Mask = mask;
drawingContext.PushOpacityMask(new ImageBrush(mask));
drawingContext.DrawRectangle(Brushes.Red, null, new Rect(0, 0, width, height));
// drawingContext.Pop();
}
target.Render(drawingVisual);
this.Rendered = target;
}
View:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<wpfApplication1:MainViewModel></wpfApplication1:MainViewModel>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderThickness="2" BorderBrush="Blue" HorizontalAlignment="Center">
<Image Source="{Binding Rendered}" Width="400" Height="400"></Image>
</Border>
<Border Grid.Column="1" BorderThickness="2" BorderBrush="Green" HorizontalAlignment="Center">
<Image Source="{Binding Mask}" Width="400" Height="400"></Image>
</Border>
</Grid>
Running the demo application produces this:
I expected the red rectangle on the left to reflect the opacity mask pushed onto the DrawingContext before drawing the actual rectangle.
The image you see on the right is the BitmapImage after the color conversion, right before it is pushed as an opacity mask. As I mentioned before I also tried it with the black/white conversion, but even with only two colors there's no effect.
I've tried many similar setups and scenarios, but I don't get the opacity mask to work.
From the Opacity Masks Overview article on MSDN (emphasis mine):
To create an opacity mask, you apply a Brush to the OpacityMask
property of an element or Visual. The brush is mapped to the element
or visual, and the opacity value of each brush pixel is used to
determine the resulting opacity of each corresponding pixel of the
element or visual.
The color values of the pixel are ignored.
I want to add an image from my Resources to an Image on the press of a Button.
Using Image1.Source = Properties.Resources.MyImage Does not work because the Image1 is of type ImageSource and the Left side is of type Drawing. How can I fix this code to display the image in my resource to the Image on button press?
This seems to work:
In app.xaml:
<Application.Resources>
<Image x:Key="myImage" Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"/>
</Application.Resources>
in MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="myGrid" Margin="5" Width="500">
<StackPanel>
<Button Content="Load Picture" Click="Button_Click_1"/>
<Image x:Name="ImageHolder"/>
</StackPanel>
</Grid>
in button click event:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Image img = Application.Current.Resources["myImage"] as Image;
ImageHolder.Source = img.Source;
}
So in your code you need to do :
Image myImage = Properties.Resources.MyImage as Image;
Image1.Source = myImage.Source;
In MainPage.xaml.cs (Silverlight Application) I can do something like this:
StackPanel myStackPanel = new StackPanel();
Button myButton = new Button();
myButton.Content = "Button";
myButton.Width = 200;
myButton.Height = 30;
Button myButton1 = new Button();
myButton1.Content = "Button 1";
myButton1.Width = 200;
myButton1.Height = 30;
myStackPanel.Children.Add(myButton);
myStackPanel.Children.Add(myButton1);
this.LayoutRoot.Children.Add(myStackPanel);
What is the equivalent of this code in a custom control when I'm trying to create these controls from the code?
Update:
My question is probably too confusing. I'l try better formulation.
So, I have
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DemoAddControlLib">
<Style TargetType="local:DemoControlShowtime">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DemoControlShowtime">
<Grid x:Name="LayoutRootControl">
<Button x:Name="Button1" Content="Hi" Width="150" Height="30"></Button>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
And code:
DemoControlShowtime.cs
[TemplatePart(Name = "Button1", Type=typeof(Button))]
public class DemoControlShowtime : Control
{
public DemoControlShowtime()
{
this.DefaultStyleKey = typeof(DemoControlShowtime);
}
// Events
public override void OnApplyTemplate()
{
Button1 = (Button)GetTemplateChild("Button1");
}
private Button button1;
private Button Button1
{
get { return button1; }
set
{
if (button1 != null)
{
Button1.Click -= new RoutedEventHandler(myButton_Click);
}
button1 = value;
button1.Click += new RoutedEventHandler(myButton_Click);
}
}
void myButton_Click(object sender, RoutedEventArgs e)
{
Button1.Content = "Hello Button";
}
}
If I click on Button1 the Content changes from "Hi" to "Hello Button". I want, when Button1 is clicked, to add StackPanel with two buttons as its Children into the Grid LayoutRootControl.
I know there is Visibility property and put it into the xaml would be easier but I'm curious how to do it from the code.
I hope this is much clearer than the question was before.
The code isn't really any different to what you have. The only variation is that the field LayoutRoot is not created for you.
However with this line of code:-
Grid LayoutRoot = GetTemplateChild("LayoutRootControl") as Grid;
The rest of your code would be identical (although you should test whether LayoutRoot is null first).
It appears to me that your are just wondering how to use a custom control in multiple places.
I've created a custom control (MyCustomControl) that has the StackPanel shown in your code, then used it multiple times on the MainPage.
MyCustomControl.xaml
<UserControl x:Class="SilverlightApplication2.MyCustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<Button Content="Button 1" Height="30" Width="200"/>
<Button Content="Button 2" Height="30" Width="200"/>
</StackPanel>
MyCustomControl.xaml.cs
public partial class MyCustomControl : UserControl
{
public MyCustomControl()
{
InitializeComponent();
}
}
Then I've used that custom control twice in the main view.
MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:SilverlightApplication2"
d:DesignHeight="300" d:DesignWidth="400">
<StackPanel>
<local:MyCustomControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<local:MyCustomControl Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
MainPage.xaml.cs
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
Output
In WPF, tb.HorizontalContentAlignment = HorizontalAlignment.Center works:
WPF XAML:
<Window x:Class="TestText2343434.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel x:Name="MainContent" Margin="10" HorizontalAlignment="Left"/>
</Grid>
</Window>
WPF Code Behind:
using System.Windows;
using System.Windows.Controls;
namespace TestText2343434
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TextBox tb = new TextBox();
tb.Width = 30;
tb.MaxLength = 1;
tb.HorizontalContentAlignment = HorizontalAlignment.Center;
MainContent.Children.Add(tb);
}
}
}
In Silverlight, tb.HorizontalContentAlignment = HorizontalAlignment.Center does not work:
Silverlight XAML:
<UserControl x:Class="TestContent222.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<StackPanel x:Name="MainContent" Margin="10" HorizontalAlignment="Left"/>
</Grid>
</UserControl>
Silverlight Code Behind:
using System.Windows;
using System.Windows.Controls;
namespace TestContent222
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
TextBox tb = new TextBox();
tb.Width = 30;
tb.MaxLength = 1;
tb.HorizontalContentAlignment = HorizontalAlignment.Center;
MainContent.Children.Add(tb);
}
}
}
What do I have to do to make HorizontalContentAlignment work in Silverlight as it does in WPF?
HorizontalContentAlignment is not a valid property on a TextBox in Silverlight (it is not exposed in Expression Blend or the VS2010 properties window).
You want to set the equivalent of TextAlignment="Center" which is
tb.TextAlignment = System.Windows.TextAlignment.Center;
Hope this helps.