This is odd. I have a Textblock (call it ErrorMessage_Textblock) in .xaml and when I tried to access and change the text of it in .xaml.cs, it throws me an error saying "The name 'ErrorMessage_Textblock' does not exist in the current context"
Basically, the ErrorMessage_Textblock is suppose to be empty when the program runs. When the user clicks the Start Button, my code in .xaml.cs checks to see if the user filled all the necessary information in the Textboxes. If there are some missing information, it will pass on a string to the ErrorMessage_Textblock -- like "please enter where to save the files."
SideMenuControl.xaml:
<UserControl x:Class="Fasetto.Word.SideMenuControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup- compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Fasetto.Word"
xmlns:core="clr-
namespace:Fasetto.Word.Core;assembly=Fasetto.Word.Core"
mc:Ignorable="d" d:DesignWidth="900" d:DesignHeight="1000"
Background="#FF2D2D30">
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<Border>
//some code ...
<Button
Click="StartButton"
Content="Start" FontSize="30" Padding="1,1,1,1" Foreground="Lime"
BorderBrush="Red"
/>
<TextBlock x:Name="ErrorMessage_Textblock" Foreground="OrangeRed"
FontFamily="/VIL_GUI_V5.0;component/Fonts/#Lato Light"
Margin="50,10,50,510" FontSize="20"
/>
SideMenuControl.xaml.cs: (note: line 7 throws me an error in VS)
public void StartButton(object sender, RoutedEventArgs e) {
if (Fasetto.Word.Core.IoC.Settings.Monaco_Report_Type.EditedText
== null || Fasetto.Word.Core.IoC.Settings.Monaco_Report_Type.EditedText == "")
{
//do something
ErrorMessage_Textblock = "please enter Report Type (Monaco)";
}
I found my own answer. You already have access to a Button, so you can find the grid it belongs to. Then, you can find the TextBlock. Only thing is that the TextBlock belongs to the same grid as the Button.
For the complete guide and code, click this link:
https://stackoverflow.com/a/35484118/10772348
Related
I want to manually control the behavior of InputPane to prevent it from showing or hiding automatically.
In my page that I put its image top, I want to InputPane show as user navigate to the page and keep showing until he/she clicks on specified button and prevent it from hiding if user clicks anywhere else in the page.
Also I want to InputPane remain hidden even if user clicks on TextBox.
I already know that there are TryShow() and TryHide(), but i can't revent auto showing and hiding.
The easy way to control it is by controlling focus of your TextBox. If you set IsTabStop on the TextBox to false - it won't take focus and so the SIP won't show up. If it already has focus - you'll need to move it out. If you want to display the SIP - focus the TextBox. Note that for performance reasons and also to prevent user confusion - it might make sense to use a TextBlock instead of a TextBox when the control should not be editable.
XAML
<Page
x:Class="App18.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App18"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<TextBox
x:Name="myTextBox"
IsTabStop="False"
AcceptsReturn="True"
VerticalAlignment="Stretch"
TextChanged="MyTextBox_OnTextChanged"/>
<Button
x:Name="myButton"
Grid.Row="1"
Click="ButtonBase_OnClick">Edit</Button>
</Grid>
</Page>
C#
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App18
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
myTextBox.IsTabStop = true;
myTextBox.Focus(FocusState.Programmatic);
}
private void MyTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text.ToLower().Contains("done"))
{
myTextBox.IsTabStop = false;
myButton.Focus(FocusState.Programmatic);
}
}
}
}
I recognize that this question is similar to others, but the others have not worked in my situation. I am trying to Two-Way Bind a textbox in my WPF to an XML file.
The data comes in perfectly into the textbox, but when I edit the textbox, the XML file is never changed. Based on what I have found online, my code seems like it should work. Here it is:
MainWindow.xaml
<Window x:Class="Learning_0._002.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window"
WindowStartupLocation="CenterScreen"
Height="400" Width="950">
<Grid>
<Grid.Resources>
<XmlDataProvider x:Key="BusinessInfo" Source="BusinessData.xml" XPath="/Businesses/Business"/>
</Grid.Resources>
<Grid x:Name="BusinessInfo" DataContext="{StaticResource BusinessInfo}">
<TextBox Name="Name" Grid.Row="0" Grid.Column="1" Text="{Binding XPath=#Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="106,93,717,250"/>
</Grid>
</Grid>
BusinessData.xml
<?xml version="1.0" encoding="utf-8" ?>
<Businesses>
<Business Name="Sample Company" Address="1234 East Road St. City, California 90068" Phone="555-555-5555" Fax="555-555-5556" Email="myemail#example.net" Website="www.example.com"/>
</Businesses>
I am new to this, and cannot find my error. Any corrections are appreciated!
Here's a question that is very similar to yours -
WPF two-way binding XML
It looks like what you need to do is instead of using a Grid.Resources you need use a datacontext instead. If you're going to be doing more advanced work I would recommend you use a class that contains all the data behind your UI elements. View this MSDN for more information - http://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx
<Grid.DataContext>
<XmlDataProvider x:Name="XMLData" Source="BusinessData.xml" XPath="/Businesses/Business"/>
</Grid.DataContext>
<Grid x:Name="BusinessInfo" Margin="98,49,118,144">
<TextBox Name="Name" Text="{Binding XPath=#Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextChanged="Name_TextChanged" />
</Grid>
And then in C# you will save it whenever they enter text into the textbox
private void Name_TextChanged(object sender, TextChangedEventArgs e)
{
XMLData.Document.Save("XMLFile1.xml");
}
You should know that when you save the file it will save to the same directory as where you run your executable. You can of course change where you save it to be the actual source of the XML.
I have text boxes that are getting URL inside, when you put the URL (long) in it, I want it to go down one row in order to see the last character of the URL.How can I achieve it instead
of changing the width size?
The TextBlock class features the TextBlock.TextTrimming Property, which enables users to add an ellipsis (...) at the end of text that is too long to be displayed in the TextBlock. If your TextBox is not being used for text input, then you can simply use a TextBlock control instead.
If you really need to use a TextBox, then unfortunately that has no such property. One alternative is to use a custom TextBox that does have this property. You can find an example of that in the WPF TextBox With Ellipsis page on CodeProject.
UPDATE >>>
As you have not shown any code, nobody can tell you what you did wrong. Either way, this is a simple issue that I'm sure that you can fix yourself. Add this to a different view somewhere else:
<TextBlock Text="123456789012345678901234567890123456789012345678901234567890"
Width="100" TextTrimming="WordEllipsis" />
Now you should be able to see the ellipsis at the end of the TextBlock. That's how simple it is. If you example is not working, then you have made it not work by adding something else.
Try scrolling the text box to the beginning of the text when focus lost (not sure how to do that with data binding):
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).ScrollToHome();
}
You can also create a Behavior to avoid direct event handling:
Add reference to System.Windows.Interactivity (installed with Expression Blend).
Add a Behavior class:
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace WpfApplication2
{
public class AutoScrollToHomeBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
AssociatedObject.LostFocus += (tb, args) =>
{
(tb as TextBox).ScrollToHome();
};
}
}
}
Attach a Behavior to your text box:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<TextBox HorizontalAlignment="Left" Height="23" Width="120">
<e:Interaction.Behaviors>
<local:AutoScrollToHomeBehavior />
</e:Interaction.Behaviors>
</TextBox>
<TextBox HorizontalAlignment="Left" Height="23" Width="120">
<e:Interaction.Behaviors>
<local:AutoScrollToHomeBehavior />
</e:Interaction.Behaviors>
</TextBox>
</StackPanel>
</Grid>
</Window>
What is the SIMPLEST way of implementing list view multiple select scenario together with the AppBar? So that it behaves exactly as the Windows 8 start screen when multiple items selected (e.g. via the mouse right-click).
I want to show the app bar together with the first selected list view item, I want to keep it opened with the second, third and so on and I want to close it either by any app bar button action (context action performed) or by other system wide app bar close action (e.g. right-click somewhere else, which would mean context action cancelled).
My current implementation is too complicated. I believe I must have missed something - such a basic and common scenario must be possible to implement in a standardized way.
Scaffolding code prepared below. If only this code used the app bar hides before right-click on the second list view item and one more right-click on list view is required (not acceptable). If combined with IsSticky it is not possible to select the second list view item at all.
<Page
x:Class="ListViewAndAppBar.ExamplePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ListViewAndAppBar"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding ExamplePageViewModel, Source={StaticResource Locator}}">
<Grid Background="Gray">
<ListView
x:Name="ListView"
ItemsSource="{Binding Persons}"
SelectionMode="Multiple"
SelectionChanged="ListView_SelectionChanged">
</ListView>
</Grid>
<Page.BottomAppBar>
<AppBar x:Name="BottomAppBar" Padding="10,0,10,0">
<Button x:Name="BottomAppBarBack" Tag="Back" Style="{StaticResource BackAppBarButtonStyle}" HorizontalAlignment="Left" />
</AppBar>
</Page.BottomAppBar>
</Page>
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.BottomAppBar.IsOpen = true;
//this.BottomAppBar.IsSticky = true;
}
Answering my own question. I found the solution short after I posted the question. I will leave it here in case somebody does the same beginner's mistake.
The solution cannot be simpler: IsSticky must be called BEFORE IsOpen. After this switch all works as expected.
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.ListBox.SelectedItems.Count > 0)
{
this.BottomAppBar.IsSticky = true;
this.BottomAppBar.IsOpen = true;
}
else
{
this.BottomAppBar.IsOpen = false;
this.BottomAppBar.IsSticky = false;
}
// Or the following if you wish...
// this.BottomAppBar.IsOpen = this.BottomAppBar.IsSticky = this.ListView.SelectedItems.Count > 0;
}
I have defined one Thickness resource in Window's resources collection which is set to value 10 along all sides. I have 3 Buttons in that window.
Upon clicking the third button, I am fetching the value of that resource, changing it(200, all edges) and applying it statically for first button and dynamically for second but still it's picking up the old value(10) for the button which is using it dynamically. For Buttton using it statically it was supposed to fetch the old value (10) but I thought just because the second button is fetching it dynamically, it will reflect the change(200).
<Window x:Class="WpfApplicationUnleashed.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplicationUnleashed"
Title="Window1" >
<Window.Resources>
<Thickness x:Key="BadiThickness">10</Thickness>
</Window.Resources>
<StackPanel>
<Button x:Name="cmdStatic" HorizontalAlignment="Center" >
I am Static
</Button
<Button x:Name="cmdDynamic" HorizontalAlignment="Center" >
I am Dynamic
</Button>
<Button x:Name="cmdChanger" HorizontalAlignment="Center" Click="cmdChanger_Click">
I am Changer
</Button>
</StackPanel>
</Window>
Code:
private void cmdChanger_Click(object sender, RoutedEventArgs e)
{
Thickness th = (Thickness)this.FindResource("BadiThickness");
th.Bottom = 200;
th.Top = 200;
th.Left = 200;
th.Right = 200;
cmdDynamic.SetResourceReference(Button.MarginProperty, "BadiThickness");
cmdStatic.Margin = (Thickness)this.FindResource("BadiThickness");
}
You do realize that Thickness is a value type and that is why when you change it's value, it won't be affected in the resource.
What you can do to set that resource's value is following:
this.Resource["BadiThickness"] = new Thickness(200);
On a side note, please avoid using Hindi in a resource's name. That may mislead.