Slider control shows abnormal behaviour on {Binding} with TextBlock - xaml

Sorry if title is weird , because the problem itself is kinda weird.
I have a TextBlock and Slider Control on Clientside.
The Slider is {Binding} to Textblock.
This is the code for them -
<TextBlock Text="{Binding Value}" x:Name="Name1Value" FontSize="25"/>
<Slider ValueChanged="slider_ValueChanged_1" x:Name="slidervalve" Value= "{Binding ElementName=Name1Value,Path=Text}" StepFrequency="25" />
Now when Slider Value changes,Slider calls a method slider_ValueChanged_1,which sends its value to server. The server accepts the value and sends the new value back to client and this new value from server is set to the textblock( x:Name="Name1Value") through an observablecollection with inotifypropertychanged implemented. The slider is {Binding} to textblock.
1)Now the first time i change the value on slider on client, the server accepts the new value and returns the new value and i am setting the new value to textblock.
2)Now i am changing the value on server and the value is correctly updated in textblock and then the slider value changes to textblock value.
3) now after this first cycle,if i change the value on server again ,the textblock on client is getting updated but the slider does not change to textblock value.
But the slider is bound to textblock. How can i solve this?
Edit1: FYI , the textblock and the slider are in same page but in different stackpanels

A Slider control's Value property is of type double, while TextBlock.Text is a string. Binding these two together without a converter is asking for trouble. You should bind your Slider.Value to the DataContext/view model and typically make sure the binding Mode=TwoWay.

Related

Textbox scroll to bottom in Avalonia

I have a textbox that I write info to that automatically creates a scroll bar when the screen is filled, and I'd like to scroll to the bottom of it. It seems like what I need to do is set the scrollbar Offset to some vector, but my problem is finding the ScrollViewer.
I can't use FindControl because it's not named anywhere in the xaml, and I can only change a few values using textbox.SetValue
The only way to scroll a TextBox currently is to set the CaretPosition. For example to scroll to the end of the text you could use:
textbox.CaretIndex = int.MaxValue;
For me it works only when I set textbox.CaretIndex to the length of the text. Assuming that property Logs (string type) is binded to textbox I used this
textbox.CaretIndex = Logs.Length

Visibility converter based on another element width

I have a Silverlight application and I use MVVM.
I would like to display or hide a TextBlock, based on another element current width (or window width at least...). The problem is that I currently have 2 texts on each other if I reduce the width of my window so I need want to hide the second textblock in that case...
I'm a beginner and I know how to use a VisibilityConverter with a boolean from my ViewModel, but not like this...
To bind to a property of another element, you would do something like this:
Visibility="{Binding ActualWidth,
ElementName=TheThingWhoseWidthYouAreBindingTo,
Converter={StaticResource WidthToVisibilityConverter}}"
And then create yourself a WidthToVisibilityConverter that takes the width value and returns a Visibility value.

Datbinding element in list to an item outside of the list

I'm using XAML for a windows phone apps, and I'm binding a collection to graph control within the listbox Itemtemplate. Everything shows correctly except that the Horizontal axis is scaled per the data in the grid and I want them all scaled the same.
I've worked out the Maximum value - which need to establish the data binding - as its in a listbox itemtemplate I can't simply set the value in code. And for the data binding I only seem to see the elements in the collection but the value I have is not an element of the collection.
Is it possible to bind to a property/field of the page - that way I can set the value and all the grids can just bind to this property/fields.
One possible way to bind element inside ListBox item template to property of the page is by using ElementName binding. Simply name the page, then you can set binding source by page name, for example :
<phone:PhoneApplicationPage
..........
..........
x:Name="myPage">
<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock Text="{Binding ElementName=myPage, Path=Name}" />
</Grid>
</phone:PhoneApplicationPage>
Above sample will display text "myPage" in the TextBlock. That way of binding also works for elements inside DataTemplate for ListBox item.

add image to the content of a column in Infragistics XamGrid, Silverlight

I am using Silveright 4. I have created a xaml page which has an Infragistics XamGrid. The grid has a column called "status" which has 3 values as: completes, started and ongoing. I want to add an image beside the content in the status column from code behind.
I have done something like this:
XamGrid1.columns[2].key
but this returs me the column name but not the content. how can i access the content the the column.. please suggest
Yes.. I want to put a if else condition on the content of the column. suppose the value is "start" then it shoud have a red flag beside it. and if it has a value as "completed" then it should have a green flag.. something like this i want to do.
To have an image in the grid cell you will need to either use a Template Columns or a create your own custom column.
If you use a Template Column, you would add the image to the template and you should then be able to use a binding to the value with a converter to get the correct image in the cell. Refer to Create a Template Column in the help for how to create a Template Column.
If you wish to create a custom column, then you would follow the approach that Devin Rader blogged about in his post titled Creating Custom Columns for the xamGrid.
Here is the a sample of the Template Column and the link to the xamGrid doc. In the Converter is where it chooses the image. the converter will return an image if true and if null or false it will return null.
http://help.infragistics.com/NetAdvantage/Silverlight/Current/CLR4.0/?page=xamGrid_Create_a_Template_Column.html
<ig:TemplateColumn Key="ISFAVORITE"
Width="auto"
HeaderText="Favorite">
<ig:TemplateColumn.ItemTemplate>
<DataTemplate>
<Image HorizontalAlignment="Center" Source="{Binding ISFAVORITE, Converter={StaticResource selectFavImageConverter}}" />
</DataTemplate>
</ig:TemplateColumn.ItemTemplate>
</ig:TemplateColumn>

Silverlight 4 MVVM ComboBox data binding is not being displayed

I have a ComboBox with the following XAML
<ComboBox Name="CompanyComboBox"
ItemsSource="{Binding Path=CompanyList, Mode=OneWay}"
SelectedItem="{Binding Path=CurrentCompany, Mode=TwoWay}"
DisplayMemberPath="Name" />
Problem:
The selected option on 'company' is persisted, but never gets displayed on load. What's missing or going wrong, or what have I forgotten to do?
CompanyList has data, and the ComboBox does get populated
The selection on the ComboBox does save to the database via the TwoWayBinding
More code is on pastebin.com, the ViewModel and the Company class code.
I have tried the following suggestions, that have so far not solve the issue:
Two-way bind a combobox to a simple string array Order of ItemSource and SelectedValue properties on were correct
ComboBox.SelectedValue not updating from binding source alternating between 'SelectedValue' and 'SelectedIndex' - neither works
Silverlight 4 Combobox with selectedValue using MVVM-Light raising PropertyChanged before setting new value also didn't help
Adding/Removing 'IsEnabled="{Binding IsReady}' on the ComboBox didn't help either
Adding SelectedValuePath="Name" or ="Value" stopped the save from working
You need to overwrite the Company.Equals() method to return true if the object's data is the same.
By default, it only returns true if the two company objects being compared share the same spot in memory, and I am guessing that your CurrentCompany object does not point to an object in CompanyList, so the SelectedItem is being set to null
Check that the instance assigned to CurrentCompany is the actual one contained in CompanyList and not a duplicate of it.
You can try this:
after populating CompanyList in your ViewModel, set the CurrentCompany to the first company, or a dummy item that says , or depending on your context.
Can we take a look at the view model please? Until this information is not present, may give following suspections.
CurrentCompany property is not public or is not a property.
View model doesn't implement INotifyPropertyChanged interface.
Setter of CurrentCompany property doesn't contain PropertyChanged event notification.