Textbox not updating - c#

Ive tried searching, but maybe im not using the right terms to search for.
I have several textboxes that im using, and when i enter data, i see the values being updated when debugging, but they never get updated to the form.
Basically, I have a form that is used for the visuals, and then i have a class that handles all the activity. I have created the class as a resource, and I am referencing to the resource within the textboxes.
The only way that i really knew how to handle the updating of the forms was by implementing the calculations on values changing. So if I updated a property, I called the method from OnPropertyChanged(). This created issues because the values were always getting changed due to the calculations rewriting values. I then tried evaluating the changes of the new value
I.E.
public double In1
{
get{_return _in1;}
set{
if (_in1 != value)
_in1 = value;
OnPropertyChanged("In1");
}
}
regardless of anything, my problem is that i dont see the values getting written to the textbox. This is my first real endevour of using data binding so im assuming that im doing something incorrectly (clearly)
<ad:DockableContent
xmlns="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="DMC_Robot_Editor.GUI.frmAngleConvertor"
Title="frmAngleConvertor" Height="259" Width="282">
<ad:DockableContent.Resources>
<local:AngleConvertor x:Key="Converter"/>
</ad:DockableContent.Resources>
<Grid >
<GroupBox HorizontalAlignment="Stretch" VerticalAlignment="Top">
<Grid>
<ComboBox x:Name="cbInput" HorizontalAlignment="Stretch" VerticalAlignment="Top" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1" DisplayMemberPath="ValueCartesianString" SelectedValuePath="ValueCartesianEnum" IsSynchronizedWithCurrentItem="True" SelectedIndex="{Binding InputItem,Source={StaticResource Converter}}" ItemsSource="{Binding InputConvention, Source={StaticResource Converter}}" IsReadOnly="True"/>
<TextBox x:Name="tbIn1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="{Binding In1, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}" Grid.Column="0" d:LayoutOverrides="GridBox" Grid.Row="2" Width="50" TextAlignment="Center">
<TextBox.DataContext>
<local:AngleConvertor/>
</TextBox.DataContext> </Grid>
</ad:DockableContent>
public class AngleConverter()
{
private double _in1 = 0.0;
public double In1
{
get{_return _in1;}
set{
if (_in1 != value)
_in1 = value;
OnPropertyChanged("In1");
}
}
}

Try to apply UpdateSourceTrigger=PropertyChanged on your text box:
Text="{Binding Path-In1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can add to your binding UpdateSourceTrigger=PropertyChanged with your Mode TwoWay
<TextBox Name="tbIn1"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Text="{Binding In1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource DoubleToStringConverter},
Source={StaticResource Converter}}"
Grid.Column="0"
d:LayoutOverrides="GridBox"
Grid.Row="2"
Width="50"
TextAlignment="Center"
/>

your real code should have something like this:
public class AngleConverter : INotifyPropertyChanged
so i assume its just a typo in your code. you did not post your converter code, the binding in your textbox is ok. Textbox default UpdateSourceTrigger is lostfocus. so maybe UpdateSourceTrigger=PropertyChanged did what you want.

Review the binding
Does DoubleToStringConverter get called?
Is get called?
Text="{Binding In1, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}"
Move Source out of binding in into DataContext on the DockableContent.
DataContext="{Binding RelativeSource={RelativeSource self}}"
Try with no converter
Text="{Binding In1}"

Related

WPF Clear combobox text value when checkbox is unchecked

I want to clear the combobox text value or set it to another string whenever the checkbox is uncheck. Is there any good way to bind them together in xaml ?
Thanks.
Maybe this helps. It is solved with bindings:
<Grid>
<ComboBox IsEditable="{Binding ElementName=Box, Path=IsChecked}" HorizontalAlignment="Left" Margin="211,167,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem>Test</ComboBoxItem>
<ComboBoxItem>Test2</ComboBoxItem>
</ComboBox>
<CheckBox x:Name="Box" Content="Clear" HorizontalAlignment="Left" Margin="131,171,0,0" VerticalAlignment="Top"/>
</Grid>
If you want to change the text, it may be necessary to write a converter:
Text="{Binding ElementName=Box, Path=IsChecked, Mode=OneWay, Converter={StaticResource TextConverter}}"
Take a look at Microsoft Documentation about how to write a value converter.

Why is my Visibility binding not working?

I have a problem with one of my Visibility Bindings.
In my application I have a Textbox and a combobox at nearly the same place.
They are overlapping so I have two Variables in the background for their Visibility. They are in a Datagrid, so the Visibility Source will be provided from the list.
The Binding of my Combobox works absolutely fine, but the one of my Textbox doesn't
Here is my Code:
<TextBox x:Name="Textvalue"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Width="150"
BorderBrush="#FF383F55"
BorderThickness="0"
Foreground="White"
Background="#FF232734"
Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
Visibility="{Binding Path=IsPath}"/>
<ComboBox x:Name="Combobox"
VerticalAlignment="Center"
Foreground="White"
Margin="3,3,3,3"
Height="23"
ItemsSource="{Binding Path=ValueArray}"
SelectedValue="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
Style="{DynamicResource ComboBoxStyle1}"
Visibility="{Binding Path=IsCombobox}"/>
These are my Get Property for both (Both only have the Get Proberty):
public System.Windows.Visibility isPath
{
get
{
return _IsPath;
}
}
public System.Windows.Visibility IsCombobox
{
get
{
return _IsCombobox;
}
}
I hope you can help me because I don't know what's wrong here :/
Thanks in advance
What I have tried:
Tried to change the binding to a different Variable.
The property name in your code has a lower case 'i' public System.Windows.Visibility isPath and the XAML has an upper case I in binding IsPath. Change this and it should be working!
{Credit C S, I'd already started writing this}

How to retrieve the text entered in texbox inside datagrid

I have a textbox inside a datagrid in a cellediting template. I want to bind the text
entered in the textbox to textblock each cell. I tried this code but it will not worked.
Here is my xaml:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--<ComboBox x:Name="monday" Width="50" IsSynchronizedWithCurrentItem="true" Loaded="monday_Loaded" SelectionChanged="monday_SelectionChanged"></ComboBox>-->
<ComboBox x:Name="monday" Width="30" ItemsSource="{Binding Path=Subjects}" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" SelectedItem="{Binding SelectedCollectionItem,Mode=TwoWay}" Loaded="monday_Loaded" SelectionChanged="monday_SelectionChanged"></ComboBox>
<ComboBox x:Name="staff" Width="30" ItemsSource="{Binding Path=mondstaff}" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" SelectedItem="{Binding SelectedCollectionItem1,Mode=TwoWay}" Loaded="staff_Loaded" SelectionChanged="staff_SelectionChanged"></ComboBox>
<TextBox x:Name="monothers" Visibility="Hidden" Text="{Binding Path=Subjects}" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" SelectedText ="{Binding SelectedCollectionItem2,Mode=TwoWay}" Width="30" TextChanged="monothers_TextChanged"></TextBox>
<!--<ComboBox x:Name="staff" Width="50" Loaded="staff_Loaded"></ComboBox>-->
</StackPanel>
</DataTemplate>
Here is my code:
public string SelectedCollectionItem
{
get { return _SelectedCollectionItem; }
set
{
_SelectedCollectionItem = value;
RaiseProperty2("SelectedCollectionItem2");
}
}
If anyone knows how to do it, kindly help me.
I think you missed this code in your code-behind
public void monothers_TextChanged(object sender, TextChangedEventArgs e)
{
var binding = ((TextBox)sender).).GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
More details
see this discussion
How to hookup TextBox's TextChanged event and Command in order to use MVVM pattern in Silverlight
and also you need to refer this blog
Make a Silverlight TextBox update its binding on every character with a Behavior

Binding data in C# and XAML?

I'm trying to use binding with C# and a XAML SemanticZoom control in my Metro app, but honestly I'm at a loss as to how to do it. Here's the XAML code I've gotten so far by piecing together from questions, articles, etc:
<SemanticZoom x:Name="boardZoom" Height="626" Margin="10,132,10,0" VerticalAlignment="Top">
<SemanticZoom.ZoomedInView>
<GridView IsSwipeEnabled="True" x:Name="ItemsGridView">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10,10,0,0"
HorizontalAlignment="Left" Background="White">
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Width="200" Height="300"
FontFamily="Global User Interface" FontSize="40" Foreground="Black"
VerticalAlignment="Center" HorizontalAlignment="Left"/>
<Image Source="{Binding Image}" Height="60" Width="60"
VerticalAlignment="Center" Margin="0,0,10,0" Visibility="{Binding isImage}" />
<TextBlock Text="{Binding Category}" TextWrapping="Wrap" Width="200"
FontFamily="Global User Interface" Foreground="Black"
VerticalAlignment="Center" HorizontalAlignment="Left"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</SemanticZoom.ZoomedInView>
<!--Didn't include SemanticZoom.ZoomedOutView since I'm still trying to get the ZoomedIn one working first-->
</SemanticZoom>
And my C# code:
List<PinStore.pin> pins = PinStore.CopyFromStream(response.GetResponseStream()); //returns a list of PinStore.pin objects, which have name, type, Image, isImage, and Category objects
System.Collections.ObjectModel.ObservableCollection<SemanticZoomed.zoomedIn> toSource = new System.Collections.ObjectModel.ObservableCollection<SemanticZoomed.zoomedIn>(); //should I be using ObservableCollection or something like List<> here?
foreach (PinStore.pin pin in pins)
{
SemanticZoomed.ZoomedIn toAdd = new SemanticZoomed.ZoomedIn(); //class with Title, Image, isImage, and Category objects
if (pin.type == "text")
{
toAdd.Title = pin.name;
toAdd.Image = null;
toAdd.isImage = Visibility.Collapsed;
toAdd.Category = pin.category;
}
toSource.Add(toAdd);
}
ItemsGridView.DataContext = toSource;
I've not had much experience in XAML/C#, and zero experience with binding. I'm not getting any errors, but I've noticed that if I replace ItemsGridView.DataContext = toSource; with ItemsGridView.ItemsSource = toSource; a blank stackpanel shows up in the GridView, and I can't seem to find a way to fill that with the Title and Category values I specify. Thanks!
Well you should first consider creating a ResourceDictionary so you can save your item style. Then you can set the ItemStyle to the Resource Dictionary.
But regardless you need to do: ItemsGridView.ItemsSource = toSource; If you do not choose to Bind the toSource in the GridView xaml.
Also make sure the SemanticZoomed.zoomedIn object implements the INotifyPropertyChanged interface, And calls the event properly. And make sure the Title, Image, Category, etc are public properties that call the event when edited. and Also you need make sure pin.Text is an actual value. {Making sure}.
If you want to learn more about data binding check out how they do it in C# & XAML with Windows 8 {Should be the same thing}:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464965.aspx

ListBox ItemsSource Binding doesn't work

I'm developing a Windows Phome application. I have the following ListBox on a page:
<ListBox Margin="10,10,8,8" x:Name="WallList">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="ListBoxItemLayout" Background="Transparent" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.33*"/>
<ColumnDefinition Width="0.77*"/>
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Left" Margin="0" Source="{Binding ImagePath}" Height="200"/>
<StackPanel Margin="5,0,0,0" Grid.Column="1">
<TextBlock x:Name="Name" TextWrapping="Wrap" Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}"/>
<TextBlock x:Name="Comment" Margin="0,5,0,0" TextWrapping="Wrap" Text="{Binding Comment}" Style="{StaticResource PhoneTextNormalStyle}" Height="130"/>
<TextBlock x:Name="When" TextWrapping="Wrap" Text="{Binding When}" Style="{StaticResource PhoneTextTitle3Style}" VerticalAlignment="Bottom"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I'm using this to fill the ListBox on Loaded event:
this.WallList.ItemsSource = StartingWall.GetWallPosts();
And now I want to add more items programmatically when the user write down some text on a TextBox and click on a button. I want to place this text on Comment field.
I'm gooing to fill the rest fields with default data.
My question is:
How can I add more items to WallList ListBox?
Someone has suggested to do the following:
public ObservableCollection<WallPostEntry> MyWallPosts {get;set;}
// Initialize MyWallPosts to whatever
MyWallPosts.Add(new WallPostEntry("new entry"));
<ListBox Margin="10,10,8,8" x:Name="WallList" ItemsSource="{Binding MyWallPosts}">
But Binding ListBox ItemsSource doesn't work for me. I'm initializing MyWallPosts on constructor, just before InitializeComponent();, like this:
public Wall()
{
MyWallPosts = StartingWall.GetWallPosts();
InitializeComponent();
}
Any advice?
Thanks.
I see a couple wierd things:
first, you're using the itemssource binding in one place, but explicitly setting it in another? setting something in code will override/undo any bindings, so that could cause a problem (but it looks like you're setting it to the same thing so that shouldn't make a difference, but i'd remove the this.WallList.ItemsSource = StartingWall.GetWallPosts(); call entirely, and leave the ItemsSource="{Binding MyWallPosts}" in the xaml. the point of using bindings is to get rid of this kind of code)
second, you're setting mywallposts and using a binding, but not setting the datacontext on your object itself? simplest in your example would be to just add one line to your constructor:
public Wall()
{
DataContext = this;
MyWallPosts = StartingWall.GetWallPosts();
InitializeComponent();
}
My next suggestion would be to simplify until it works. leave the listbox but comment out all of the item/data templating to make sure that you don't have a bug in your template
Could he just add
a DataContext property to this:
<ListBox Margin="10,10,8,8" x:Name="WallList" ItemsSource="{Binding MyWallPosts}">
so:
<ListBox Margin="10,10,8,8" x:Name="WallList" ItemsSource="{Binding MyWallPosts}" DataContext="{Binding MyWallPosts}">
Would there be any way of setting the dataContext declarativelly?
tks,
Oscar

Categories

Resources