문제

데이터 ontabSelectionChanged를 검증하는 Silverlight 2 응용 프로그램이 있습니다. 즉시 updateSourcetrigger가 LostFocus보다 더 많은 것을 허용하기를 바랐습니다. 왜냐하면 LINQ 객체가 유효성을 유지하기 전에 업데이트되지 않기 때문에 탭을 클릭하면 탭을 클릭합니다.

나는 다른 컨트롤에 초점을 맞추고 다시 OnTextChanged로 텍스트 상자 문제를 해결했습니다.

Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
    txtSetFocus.Focus()
    sender.Focus()
End Sub

이제 나는 Datagrid 내에서 동일한 종류의 해킹을 수행하려고 노력하고 있습니다. DataGrid는 셀 테템 플레이트 및 CellEditingTemplate의 런타임에 생성 된 DataTemplates를 사용합니다. textemplate의 textbox에 textChanged = "ontextChanged"를 작성하려고했지만 트리거되지는 않습니다.

누구든지 아이디어가 있습니까?

도움이 되었습니까?

해결책

텍스트 상자에도 적용된 동작으로 수행 할 수 있습니다.

// xmlns:int is System.Windows.Interactivity from System.Windows.Interactivity.DLL)
// xmlns:behavior is your namespace for the class below
<TextBox Text="{Binding Description,Mode=TwoWay,UpdateSourceTrigger=Explicit}">
    <int:Interaction.Behaviors>
       <behavior:TextBoxUpdatesTextBindingOnPropertyChanged />
    </int:Interaction.Behaviors>
</TextBox>


public class TextBoxUpdatesTextBindingOnPropertyChanged : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.TextChanged -= TextBox_TextChanged;
    }

    void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        var bindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
        bindingExpression.UpdateSource();
    }
}

다른 팁

이 블로그 게시물은 첨부 된 속성을 사용하여 텍스트 상자의 소스를 명시 적으로 업데이트하는 방법을 보여줍니다.http://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/

다른 컨트롤과도 작동하도록 쉽게 수정할 수 있습니다 ...

MVVM과 Silverlight 4를 사용하여 동일한 문제를 겪었습니다. 문제는 텍스트 상자가 초점이 풀릴 때까지 바인딩이 소스를 업데이트하지 않지만 다른 컨트롤에 초점을 맞추는 것은 트릭을 수행하지 않는다는 것입니다.

두 개의 다른 블로그 게시물의 조합을 사용하여 솔루션을 찾았습니다. Patrick Cauldwell의 DefaultButtonHub 개념의 코드를 사용했으며 SmallWorkarounds.net의 "SmallWorkound"하나와 함께 사용되었습니다.

http://www.cauldwell.net/patrick/blog/defaultbuttonsemanticsinsilverlightrevisited.aspx

www.smallworkarounds.net/2010/02/elementbindingbinding-modes.html

내 변경 사항은 DefaultButtonHub 클래스에 대한 다음 코드를 초래했습니다.

public class DefaultButtonHub
{
    ButtonAutomationPeer peer = null;

    private void Attach(DependencyObject source)
    {
        if (source is Button)
        {
            peer = new ButtonAutomationPeer(source as Button);
        }
        else if (source is TextBox)
        {
            TextBox tb = source as TextBox;
            tb.KeyUp += OnKeyUp;
        }
        else if (source is PasswordBox)
        {
            PasswordBox pb = source as PasswordBox;
            pb.KeyUp += OnKeyUp;
        }
    }

    private void OnKeyUp(object sender, KeyEventArgs arg)
    {
        if (arg.Key == Key.Enter)
            if (peer != null)
            {
                if (sender is TextBox)
                {
                    TextBox t = (TextBox)sender;
                    BindingExpression expression = t.GetBindingExpression(TextBox.TextProperty);
                    expression.UpdateSource();
                }
                ((IInvokeProvider)peer).Invoke();
            }
    }

    public static DefaultButtonHub GetDefaultHub(DependencyObject obj)
    {
        return (DefaultButtonHub)obj.GetValue(DefaultHubProperty);
    }

    public static void SetDefaultHub(DependencyObject obj, DefaultButtonHub value)
    {
        obj.SetValue(DefaultHubProperty, value);
    }

    // Using a DependencyProperty as the backing store for DefaultHub.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DefaultHubProperty =
        DependencyProperty.RegisterAttached("DefaultHub", typeof(DefaultButtonHub), typeof(DefaultButtonHub), new PropertyMetadata(OnHubAttach));

    private static void OnHubAttach(DependencyObject source, DependencyPropertyChangedEventArgs prop)
    {
        DefaultButtonHub hub = prop.NewValue as DefaultButtonHub;
        hub.Attach(source);
    }

}

이것은 Silverlight에 대한 일종의 문서에 포함되어야합니다 :)

나는 그것이 오래된 뉴스라는 것을 알고 있습니다 ... 그러나 나는 이것을함으로써 이것을 얻었습니다.

텍스트 = "{바인딩 경로 = newQuantity, updateSourcetrigger = propertyChanged}"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top