Click here to Skip to main content
15,886,110 members
Home / Discussions / WPF
   

WPF

 
QuestionWPF databinding versus dispatcher Pin
GuyThiebaut15-Oct-13 5:03
professionalGuyThiebaut15-Oct-13 5:03 
AnswerRe: WPF databinding versus dispatcher Pin
David C# Hobbyist.15-Oct-13 7:11
professionalDavid C# Hobbyist.15-Oct-13 7:11 
GeneralRe: WPF databinding versus dispatcher Pin
GuyThiebaut15-Oct-13 7:59
professionalGuyThiebaut15-Oct-13 7:59 
GeneralRe: WPF databinding versus dispatcher Pin
Mycroft Holmes15-Oct-13 12:40
professionalMycroft Holmes15-Oct-13 12:40 
GeneralRe: WPF databinding versus dispatcher Pin
GuyThiebaut16-Oct-13 2:41
professionalGuyThiebaut16-Oct-13 2:41 
GeneralRe: WPF databinding versus dispatcher Pin
Mycroft Holmes16-Oct-13 12:43
professionalMycroft Holmes16-Oct-13 12:43 
AnswerRe: WPF databinding versus dispatcher Pin
Abhinav S15-Oct-13 18:23
Abhinav S15-Oct-13 18:23 
AnswerRe: WPF databinding versus dispatcher Pin
Kenneth Haugland16-Oct-13 2:25
mvaKenneth Haugland16-Oct-13 2:25 
I see that you alread have gotten some very useful comments in this question. I found that If I set the binding in code behind it is easyer to do ceirtain things, like:
VB
''' <summary>
 ''' Set binding from Double property to TextBox.Text
 ''' </summary>
 ''' <param name="Source">The class instance</param>
 ''' <param name="Path">Property name in the class</param>
 ''' <param name="control">The textboxcontrol</param>
 ''' <remarks></remarks>
 Sub SetObjectBinding(ByVal Source As Object, ByVal Path As String, ByVal control As TextBox)
     Dim bnd As New Binding
     bnd.Path = New PropertyPath(Path)
     bnd.Source = Source
     bnd.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
     bnd.Converter = New DoubleToStringConverter
     bnd.ValidationRules.Add(New PositiveDoubleValidator)
     control.SetBinding(TextBox.TextProperty, bnd)
 End Sub


With the classes:
VB
<ValueConversion(GetType(Double), GetType(String))>
Private Class DoubleToStringConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Return value.ToString
    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Dim d As Double
        Dim str As String = value.ToString

        If str.Contains(".") Then str = str.Replace(".", ",")

        If Double.TryParse(str, d) Then
            If d > 0 Then
                Return d
            Else
                Return DependencyProperty.UnsetValue
            End If
        End If

        Return value.ToString
    End Function
End Class

Public Class PositiveDoubleValidator
    Inherits ValidationRule

    Public Overloads Overrides Function Validate(value As Object, cultureInfo As System.Globalization.CultureInfo) As System.Windows.Controls.ValidationResult

        Dim d As Double
        Dim str As String = value.ToString

        If str.Contains(".") Then str = str.Replace(".", ",")
        If Double.TryParse(str, d) Then
            If d > 0 Then
                Return ValidationResult.ValidResult
            Else
                Return New ValidationResult(False, "The entered value must be positive")
            End If
        Else
            Return New ValidationResult(False, "The value '" & str & "' could not be converted to a positive number")
        End If

    End Function
End Class


And in the XAML code:
XML
<Style TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <Border BorderBrush="Red" BorderThickness="5">
                        <AdornedElementPlaceholder />
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>


I dont know exactly how this could be done in MVVM but this works for me at least.
GeneralRe: WPF databinding versus dispatcher Pin
GuyThiebaut16-Oct-13 2:44
professionalGuyThiebaut16-Oct-13 2:44 
GeneralRe: WPF databinding versus dispatcher Pin
Kenneth Haugland16-Oct-13 2:51
mvaKenneth Haugland16-Oct-13 2:51 
QuestionWPF Views & ViewModels Question Pin
Kevin Marois14-Oct-13 7:43
professionalKevin Marois14-Oct-13 7:43 
AnswerRe: WPF Views & ViewModels Question Pin
SledgeHammer0114-Oct-13 18:03
SledgeHammer0114-Oct-13 18:03 
GeneralRe: WPF Views & ViewModels Question Pin
Kevin Marois15-Oct-13 7:19
professionalKevin Marois15-Oct-13 7:19 
GeneralRe: WPF Views & ViewModels Question Pin
SledgeHammer0115-Oct-13 10:48
SledgeHammer0115-Oct-13 10:48 
GeneralRe: WPF Views & ViewModels Question Pin
Kevin Marois16-Oct-13 9:16
professionalKevin Marois16-Oct-13 9:16 
GeneralRe: WPF Views & ViewModels Question Pin
SledgeHammer0116-Oct-13 10:01
SledgeHammer0116-Oct-13 10:01 
GeneralRe: WPF Views & ViewModels Question Pin
Kevin Marois16-Oct-13 11:10
professionalKevin Marois16-Oct-13 11:10 
GeneralRe: WPF Views & ViewModels Question Pin
SledgeHammer0116-Oct-13 17:41
SledgeHammer0116-Oct-13 17:41 
GeneralRe: WPF Views & ViewModels Question Pin
SledgeHammer0116-Oct-13 10:09
SledgeHammer0116-Oct-13 10:09 
Questionaccessing remote system Pin
Arun kumar Gautam9-Oct-13 23:18
Arun kumar Gautam9-Oct-13 23:18 
AnswerRe: accessing remote system Pin
Richard MacCutchan9-Oct-13 23:25
mveRichard MacCutchan9-Oct-13 23:25 
GeneralRe: accessing remote system Pin
Arun kumar Gautam9-Oct-13 23:43
Arun kumar Gautam9-Oct-13 23:43 
GeneralRe: accessing remote system Pin
Richard MacCutchan10-Oct-13 0:07
mveRichard MacCutchan10-Oct-13 0:07 
AnswerRe: accessing remote system Pin
mohitsaxena110-Oct-13 7:50
professionalmohitsaxena110-Oct-13 7:50 
GeneralRe: accessing remote system Pin
Arun kumar Gautam11-Oct-13 7:00
Arun kumar Gautam11-Oct-13 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.