I have the code in XAML:
<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
<ContentControl.Content>
<Viewbox Margin="3">
<TextBlock Text="7"/>
</Viewbox>
</ContentControl.Content>
</Button>
And code behind:
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)e.Source;
txbDisplay.Text = btn.Content.ToString();
}
how do I get the value of <TextBlock Text="7"/> in btn_Click?
Based on your XAML, btn.Content should be the instance of the Viewbox. Then the Child property of the Viewbox should be your TextBlock.
So you should be able to do this (inside your button click event handler):
var viewBox = (Viewbox)btn.Content;
var textBlock = (TextBlock)viewBox.Child;
var text = textBlock.Text; // This is the value you were looking for
txbDisplay.Text = text;
Also, just as a helpful note, the <ContentControl.Content> ... </ContentControl.Content> part of your XAML is superfluous. That block could just be written as:
<Button Grid.Column="0" Grid.Row="1" x:Name="btnSete" Click="btn_Click">
<Viewbox Margin="3">
<TextBlock Text="7"/>
</Viewbox>
</Button>
See the "XAML Content Properties" section on this page for more information why that is if you're not sure.
Related
I have a TextBlock that is inside a Control Template. I want to change the Text for said TextBlock with the Text value of a TextBox. The value is meant to be set within a button click event however, with the way I've tried to do this it doesn't work. The click event will give out an error stating that text is null.
I am new to WPF and would appreciate any help.
XAML for Control Template:
<Window.Resources>
<ControlTemplate x:Key="panel" TargetType="Button">
<Grid>
<Rectangle x:Name="rectangle" Width="auto" Height="55" RadiusX="10" RadiusY="10"
Fill="White">
</Rectangle>
<TextBlock x:Name="txtBlk" Text="" Margin="10,10,0,0" />
</Grid>
</ControlTemplate>
</Window.Resources>
C# for Button_Click event:
private void panelBtn_Click(object sender, RoutedEventArgs e)
{
var text = (TextBlock)this.Template.FindName("txtBlk", this);
text.Text = txtBox.Text;
}
You should reference the template of the button like this..
private void panelBtn_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn)
{
var text = btn.Template.FindName("txtBlk", btn) as TextBlock;
text.Text = txtBox.Text;
}
}
#MuhammadSulaiman answered you correctly, but I would suggest that you change the implementation.
Rather than looking for an element in a template, it's better to add a resource to which this element will refer and change this resource.
<Window.Resources>
<sys:String x:Key="button.Text">Some Text</sys:String>
<ControlTemplate x:Key="panel" TargetType="Button">
<Grid>
<Rectangle x:Name="rectangle" Width="auto" Height="55" RadiusX="10" RadiusY="10"
Fill="White">
</Rectangle>
<TextBlock x:Name="txtBlk"
Text="{DynamicResource button.Text}"
Margin="10,10,0,0" />
</Grid>
</ControlTemplate>
</Window.Resources>
private void panelBtn_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement elm)
{
elm.Resources["button.Text"] = txtBox.Text;
}
}
You can also change the initial text in XAML:
<Button Template="{DynamicResource panel}">
<Buttun.Resources>
<sys:String x:Key="button.Text">Other Text</sys:String>
</Buttun.Resources>
</Buttun>
In the same way, you can set a common initial text for all buttons located in one common container, through the resources of this container.
i want to send Data from one Textbox on window One to a label on window Two.
starting with window two:
<StackPanel>
<StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
</StackPanel>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
Codebehind of Window Two:
public MainWindow()
{
(App.Current as App).CodeText = _jediCode;
InitializeComponent();
}
private void OnClickToDarkSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Dark);
(App.Current as App).CodeText = _sithCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToLightSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Light);
(App.Current as App).CodeText = _jediCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToGraySide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Gray);
(App.Current as App).CodeText = _grayCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
{
if (StyleWindowButton.IsChecked == true)
{
styleWindow = new StyleWindow();
styleWindow.Show();
}
else
{
styleWindow.Close();
styleWindow = null;
}
}
private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
{
if (TextWindowButton.IsChecked == true)
{
textWindow = new InputWindow();
textWindow.Show();
}
else
{
textWindow.Close();
textWindow = null;
}
}
}
window one:
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap"
AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
<!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
</TextBox>
</Grid>
code behind of one is empty.
app.xaml.cs:
public string CodeText
{
get => _codeText;
set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}
Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other.
hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label
bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)
in fact, window Two removes the binding by assigning CodeText directly as local value:
theTextBlock.Content = (App.Current as App).CodeText;
you should remove that line, and use the same binding as in window One:
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock"
Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>
I am trying to access a control from a control's context menu's control template.My xaml is :
<Button x:Name="button1" ContextMenuService.Placement="top" Content="Button" HorizontalAlignment="Left" Margin="2,543,0,0" VerticalAlignment="Top" Width="75" Grid.ColumnSpan="2">
<Button.ContextMenu>
<ContextMenu x:Name="btconmn" >
<ContextMenu.Template>
<ControlTemplate>
<Grid x:Name="newgrid" Width="183" Height="190">
<Rectangle Fill="#FF263349" x:Name="newfolder" HorizontalAlignment="Left" VerticalAlignment="Top" Width="179" Height="32" Margin="2,1,0,0"/>
</Grid>
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
</Button.ContextMenu>
</Button>
Here i'm trying to access the newfolder rectangle. So far i tried :
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var template = btconmn.Template;
var myControl = (Rectangle)template.FindName("newfolder", btconmn);
}
which returns a null reference exception.Any help ?
WPF controls usually don't load until they are needed, so in your case the earliest point at which you can access the rectangle would be after the button's contextmenu is loaded (which happens immediately before it opens for the first time):
Add this to your XAML:
<ContextMenu x:Name="btconmn" Loaded="Btconmn_OnLoaded">
And this in your code behind:
private void Btconmn_OnLoaded(object sender, RoutedEventArgs e)
{
var template = btconmn.Template;
var myControl = (Rectangle)template.FindName("newfolder", btconmn);
}
how to generate hyperlink from text writeen in textbox to textblock in windows phone 8.0 using C#
ex:- i entered
www.google.com in textbox and clicked on button after button click
the result should be
www.google.com
with hyperlink in textblock
You can easily put Hyperlink into RichTextBlock (in WP8.1 Runtime). I've also put Run in hyperlink so it's easier to manage its content. Example:
<StackPanel>
<TextBox Name="myTextBox" Width="200"/>
<RichTextBlock TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center">
<Paragraph>
<Run Text="This is a link to google:"/>
<LineBreak/>
<Hyperlink x:Name="myhyperlink" Click="myhyperlink_Click">
<Run x:Name="hyperText" Text="textInside"/>
</Hyperlink>
<LineBreak/>
<Run Text="you can click it to invoke doEvent in your code."/>
</Paragraph>
</RichTextBlock>
</StackPanel>
In the code behind - some logic example:
public MainPage()
{
this.InitializeComponent();
myTextBox.TextChanged += (sender, e) => hyperText.Text = myTextBox.Text;
}
private async void myhyperlink_Click(Windows.UI.Xaml.Documents.Hyperlink sender, Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
await Windows.System.Launcher.LaunchUriAsync(new Uri(#"http://" + myTextBox.Text));
}
Note that in WP8.0 and WP8.1 Silverlight you will have to use RichTextBox with IsReadOnly = true
Use a HyperlinkButton control.
<HyperlinkButton NavigateUri="http://www.google.com">
<HyperlinkButton.Content>
<TextBlock Text="google.com" />
</HyperlinkButton.Content>
</HyperlinkButton>
try this:
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
<StackPanel x:Name="stack">
<TextBox x:Name="txtInput"></TextBox>
<Button Content="Create Link" Click="Button_Click"/>
</StackPanel>
</Grid>
CS:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtInput.Text != "")
{
HyperlinkButton obj = new HyperlinkButton();
obj.NavigateUri = new Uri(txtInput.Text,UriKind.RelativeOrAbsolute);
obj.Content = txtInput.Text;
obj.TargetName = "_blank";
this.stack.Children.Add(obj);
}
}
for e.g. try with http://google.com in textbox
Try this
xaml
<StackPanel x:Name="stack">
<TextBlock Text="{Binding LineThree}" TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBox x:Name="txtInput"></TextBox>
<Button Content="Create Link" Click="Button_Click"/>
<RichTextBox x:Name="textBox" ></RichTextBox>
</StackPanel>
and the button click in the cs file
private void Button_Click(object sender, RoutedEventArgs e)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.Inlines.Add(txtInput.Text);
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(hyperlink);
textBox.Blocks.Add(myParagraph);
}
I have this data grid where I am placing all my buttons
<Grid x:Name="ButtonGrid" HorizontalAlignment="Left" Margin="0,90,0,4" Width="186">
<Button x:Name="B1" Content="B1" Height="18" Margin="73,0,59,16" VerticalAlignment="Bottom" Click="B1"/>
<Button x:Name="B2" Content="B2" Height="18" Margin="0,0,-2,16" VerticalAlignment="Bottom" Click="B2_Click" HorizontalAlignment="Right" Width="57"/>
</Grid>
I have the grid collapased on start. But when a button {testGrid} is clicked, I want the grid to ne visible.
Here is my code
namespace project.Test
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
EDUTED
private void testGrid_Click(object sender, System.Windows.RoutedEventArgs e)
{
FrameworkElement ButtonGrid = (sender as FrameworkElement).FindName("ButtonGrid") as FrameworkElement;
if ( ButtonGrid.Visibility == System.Windows.Visibility.Collapsed)
ButtonGrid.Visibility = System.Windows.Visibility.Visible;
else
ButtonGrid.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
I think if you move your Grid outside of your DataTemplate it will work. :)
However if you really need to put it in a DataTemplate, as long as your Button is at the same level as the Grid, you should still be able to find it.
Say your xaml code looks like this,
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="controlstoryboardactionrefissue.MainPage" Width="640" Height="480">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid x:Name="myGrid" Height="128" Background="#FFE7C0C0" Width="333">
<Button x:Name="myButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="31,29,0,0" Click="myButton_Click" />
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ContentControl HorizontalAlignment="Left" VerticalAlignment="Top" Margin="175,198,0,0" ContentTemplate="{StaticResource DataTemplate1}" />
</Grid>
</UserControl>
Then the code behind,
private void myButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myButton = (Button)sender;
var grid = myButton.Parent as Grid;
if (grid != null)
{
// do stuff
}
}
Hope it helps. :)