I'd like to know if it is possible to define as the text of a Button in WPF, something like:
a b c
I've tried setting
alt text http://img651.imageshack.us/img651/1838/ctldhrzhy41gbrcch4dpjz4.png
but that doesn't seem to work.
Is it only possible to use the Bold tag with FlowDocuments?
Thanks
Use a TextBlock to hold the formatted text:
<Button>
<TextBlock>Hey <Bold>you</Bold>!!!</TextBlock>
</Button>
Per your comment, if you want to be explicit about the fact that this sets the Content property, you can use XAML property element syntax to do so:
<Button>
<Button.Content>
<TextBlock>Hey <Bold>you</Bold>!!!</TextBlock>
</Button.Content>
</Button>
However this is redundant because Button has a ContentPropertyAttribute which makes the first version exactly equivalent to the second anyway.
This will work.
<Grid>
<Button Name="button1" Width="40" Height="40"
Content="something" FontWeight="Bold" />
</Grid>
Try <Button><TextBlock>a<Bold>b</Bold>c</TextBlock></Button>.
The simpliest solution i could think of:
private void ButtonClick(object sender, RoutedEventArgs e)
{
string buttonText = (sender as Button).Content.ToString();
}
Related
First I've tried to implement a Click Event to my Textbox. Unfortunately, it doesn't work with XAML.
So, my plan is to add a Button and whenever you click this Button, the textfield below should change it's letter (back to 1).
My idea was to put the button over the first textbox and to hide it, so that you see the first textbox.
But, if I set the button as hidden, my function doesn't work anymore.
Is there a solution to hide the button, but, still keep the function for the second textbox?
Hidden means control is loaded, takes up space on the screen, but won't be operational(clickable), so it doesn't help you.
You could edit the button's ControlTemplate and make it a simple Grid with Transparent background, without the Hidden part of course.
And last thing, you could add MouseDown function on your TextBox so you won't need the button at all.
If you use bindings and commands you have two ways:
<Button Command="{Binding ClickCommand}">
<Button.Template>
<ControlTemplate TargetType="Button">
<TextBlock Text="Some text"/>
</ControlTemplate>
</Button.Template>
</Button>
or
<TextBlock Text="Some text">
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ClickCommand}"/>
</TextBlock.InputBindings>
</TextBlock>
Got it!
.xaml:
<TextBox MinWidth="90" x:Name="txtBoden" TextChanged="TxtBoden_TextChanged"
PreviewMouseDown="txtBoden_MouseDown"></TextBox>
.cs:
public void txtBoden_MouseDown(object sender, MouseButtonEventArgs e)
{
txtFach.Text = "1";
}
I would like to know a way to make some text appear when you press a button.
I've already created a button and a text box:
<Button Height="25" Width="200" Click="Button_Click" Content="Press this button"/>
<TextBlock Height="50" Width="300" Margin="243,147,249,222" TextAlignment="Center" FontSize="30"/>
Also, I've created a boolean for when you press the button, I donĀ“t know if this is necessary or not.
private void Button_Click(object sender, RoutedEventArgs e)
{
Boolean button = true;
}
Basically, like MaxB said, every control in WPF has a "Visibility" property, that you can change between Visible, Collapsed or Hidden.
Since you already have a Handle for the Button_Click event, all you need to do now is give a name to your TextBlock with the x:Name property like-so :
<TextBlock x:Name="MyTextBlock"/>
Then, in the code of your handler, you can choose which Visibility to apply to the TextBlock according to the state of your boolean.
You can access the TextBlock properties by the name you gave it in the XAML file, like-so :
this.MyTextBlock.Visibility = Visibility.Hidden, for example.
You didn't create a textbox you created a textblock. Firstly create a textbox and give it a name. Then on your Button_click method you can write NameOfTextBox.Text = "Your text";
Just started in c# and want to create simple UWP application, i have TextBlock and Button, i want when press Button TextBlock text changes but always get error:
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBlock Text = "HELLO";
}
CS0029 C# Cannot implicitly convert type 'string' to 'windows.ui.xaml.controls.textblock'
I am confused, why Text field don't understand string.
Thanks for helping.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Button" HorizontalAlignment="Left" Height="50" Margin="105,306,0,0" VerticalAlignment="Top" Width="146" Click="Button_Click"/>
<TextBlock HorizontalAlignment="Left" Margin="56,141,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="107" Width="241" SelectionChanged="TextBlock_SelectionChanged" FontSize="36"/>
</Grid>
xaml:
<Grid>
<Button Name="btn" Click="Button_Click" Content="Button"/>
<TextBlock Name="tb"/>
</Grid>
xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e)
{
tb.Text = "HELLO";
}
or remove Click event and put this on constructor
btn.Click += (se, arg) => tb.Text = "HELLO";
by the way, TextBlock doesn't have SelectionChanged event
To explain what Christopher made above (his code is correct):
He added an identifier in the XAML code to the TextBlock object.
Name="tb"
And then in the C# code he used the identifier to refer to the TextBlock object.
Since you said you're new to C#, let me give you a golden advice. Always give variabels and objects (such as buttons, textboxes etc.) names (identifiers) in relation to their assignment. That way it's much easier for you to use them later in the code.
A little tip to shorten a few commonly used named objects:
Button = btn
Textbox = tbx
Label = lbl
Recently I had been looking for a way to make the tabs in a TabControl editable and came across This example on telerik's website. That did exactly what I wanted but it got me thinking about a similar usage for buttons. I was wondering if it would be possible to use something like that and make a button that would show a textbox instead of the content presenter when say, you right click the button? I tried to make something like this work but so far have only ended up with a blank button.
<Button x:Name="SB" Height="222" Width="222" Click="SB_Click">
<ContentControl.ContentTemplate>
<DataTemplate>
<local:SuperButton Content="{Binding Path=x, Mode=TwoWay}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</Button>
Where x is a string variable and using the code behind from the link above (with a class name change, of course).
edit: This button will be in an itemscontrol, so I don't think naming the inner elements in xaml will work, but I do like the ease of Wolfgang's answer.
The WPF Content Model is really flexible and allows literally anything inside anything.
This is perfectly valid XAML:
<Button>
<TextBox/>
</Button>
Or even:
<Button>
<MediaElement Source="C:\Videos\WildLife.wmv"/>
</Button>
You can simply host a (e.g.) label (TextBlock) with the text AND a TextBox inside the Button and set their Visiblity properties.
That way, if you right click the button, the TextBox shows up.
<Button>
<Grid>
<TextBox Text=normal button caption" x:Name="label" />
<TextBox
x:Name="textbox"
Text="visible on right click"
MouseRightButtonDown="HandleRightClick"/>
</Grid>
</Button>
And then in your C# code create an event handler to set the Visiblity correctly.
void HandleRightClick(object sender, MouseButtonEventArgs e)
{
label.Visibility = Visibility.Collapsed;
textBlock.Visibility = Visibility.Visible;
}
<Button x:Name="btn_binding" Content="Binding" HorizontalAlignment="Right" Height="44" Margin="0,127,63,0" VerticalAlignment="Top" Width="67"/>
<TextBox x:Name="txt_binding" Text="{Binding Content,ElementName=btn_binding}" Height="48" Margin="0,48,31,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Right" Width="130"/>
it's result will be like this
Then i can get the same result by the below code
public partial class biding : Window
{
public biding()
{
this.InitializeComponent();
txt_binding.Text=btn_binding.Content.ToString();
}
}
please tell me what are the differences of both and i want to which one is best to use...
In first case you bind your TextBox.Text property to Button.Content property and it will be changed everytime the Content is changed. In second you just set Text property once in constructor and changing the Button.Content won't affect into it.
Hope it's clear.
In my point of view... Xaml binding will be reflected in the designer at the time when you typed... No need to wait to run the solution to see the output..
In case of code behind .. you need to run the solution to see the results..