64888

How to set Alignment of a textbox.Text in wpf via INotify-Property?

Question:

My WPF Application code generates panels on function call defined in .cs file. There is ItemControl used in code to generates these Panels . I want to to change text Alignment of text of Textbox defined in selected panel via it's button. Query: I clicked button and alignment of select panel TextBox change from left to rightand from right to left, Now implementation of Alignment sets if select slider to move. Here code is:

<strong>XAML FILE</strong>

<ItemsControl x:Name="lstItemsClassM"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <Button Content="{Binding Alignment, Mode=TwoWay}" Click="Button_Click" Tag="{Binding PKId}" /> <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" TextAlignment="{Binding Alignment, Mode=OneWay}" /> <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" /> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate>

<strong>.CS FILE</strong>

protected ObservableCollection<ClassM> texts = new ObservableCollection<ClassM>(); int dv; public Window2() { InitializeComponent(); dv=1; texts.Add(new ClassM() { PKId=dv, Text = "Test 1" }); dv=2; texts.Add(new ClassM() { PKId=dv, Text = "Test 2" }); lstItemsClassM.ItemsSource = texts; } private void Button_Click(object sender, RoutedEventArgs e) { var myValue = ((Button)sender).Tag; foreach (var f in texts.ToList()) { if (f.PKId.ToString() == myValue.ToString()) { f._alignment = "Right"; MessageBox.Show(f._alignment); } } } } public class ClassM : INotifyPropertyChanged { private string _id; private int _pkid; private string _text; private double _fontSize = 10; public string _alignment="Left"; public int PKId { get { return _pkid; } set { if (value != _pkid) { _pkid = value; NotifyPropertyChanged(); } } } public string Id { get { return _id; } set { if (value != _id) { _id = value; NotifyPropertyChanged(); } } } public string Text { get { return _text; } set { if (value != _text) { _text = value; NotifyPropertyChanged(); } } } public double FontSize { get { return _fontSize; } set { if (value != _fontSize) { _fontSize = value; NotifyPropertyChanged(); } } } public string Alignment { get { return _alignment; } set { if (value != _alignment) { _alignment = value; NotifyPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

}

<strong>Alignment means Textbox.Text Alignment left to right or right to left</strong>

Answer1:

The best solution is use RichTextBox to align your text. If you wanna i could sugest one implementation to read a literal string and format.

Recommend

  • Find the distance of each pair between two vectors
  • Verify Encoding tags by Zebra printer RZ400
  • XSLT new lines not being preserved
  • Xquery append text to tag values depending of the element type
  • PDF File generated by POST Request Not Opening
  • weblogic jdbc Datasource error BEA-001131 Closed Connection
  • pandas left join where right is null on multiple columns
  • Dropping factors which levels have observations smaller than a specific value-R
  • Adding Two Numbers from Input
  • Remove Special Chars from a TSV file using Regex
  • Are the data registers EAX, EBX, ECX and EDX interchangeable
  • XSL-FO add new line after each node
  • How to exclude linebreak-only textnodes from text() XPath query?
  • How to split a file name by dot and get the 2 last portion
  • Counting distinct items in XSLT and listing only once
  • Ray-Sphere intersection [closed]
  • Implement custom JTA XAResource for using with hibernate
  • Illegal character in Xml
  • Impossible to inject JMS QueueConnectionFactory in JBoss 6.1.0 with resource annotation
  • ORA-24778: cannot open connections
  • escaping \" symbol used in findstr within a FOR statement
  • Multiple rows using XSLT
  • How to make side ID navigation links active/hover state as I scroll down to their div
  • Generate KeyPair with RandomSecure
  • Alternating table row color with the first row a different color
  • Deserializing nested JSON C# and Bind to XAML ListBox
  • Get XML Attributes in WPF with a TreeView
  • WPF Binding View as Content
  • View Injection inside ItemsControl
  • UWP ResourceDictionary is not being loaded when I add a second object to it
  • Not able to stretch an inner StackPanel
  • Nested INotifyPropertyChanged class won't work
  • UWP Template 10 create a dynamic hamburgermenu
  • How to create a WPF-like data trigger in Silverlight?
  • Opening a Access 2007 file
  • How to create a fast loading wrapping ListBox?
  • iOS UIWebView: How can I figure out what the previous URL is?
  • WPF8/C# - Binding Data To Grid
  • Diagonal of polygon is inside or outside?
  • Float Label hint (TextInputLayout) not works with Android Data Binding
  • Could be Text orientation of JTextArea changed by keyboard shortcut?
  • How to change display text in item template based on eval in gridview?
  • jqPlot date axis - bars drawn on wrong day?
  • Complex Silverlight TreeView, is nested hierarchy possible?
  • Multibinding Multiselection ListView
  • my tic-tac-toe program in matlab does not work [closed]
  • C# List of Panels
  • Using MouseListener to select a range of cells in a grid
  • onBackPressed() not being executed
  • Android fill_parent issue
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Symfony2: How to get request parameter
  • jquery mobile loadPage not working
  • Google cloud sdk not working when python points python3
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Why winpcap requires both .lib and .dll to run?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • how to add data labels for bar graph in matlab
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • coudnt use logback because of log4j