2016-04-04 15 views
1

Çok sayıda araç ipucuna sahip büyük bir uygulamam var. Araç ipuçları, sürekli değişen verilerin anlık görüntüsünü gösterir. Şu anda, verilerin bir anlık görüntüsünü yoklayarak ve ardından özellikleri ayarlayarak ve INotifyPropertyChanged desenini izleyerek yineliyorum. Bu çalışır, ancak sonuç, kullanıcının şanssız olmaları durumunda verilerin yenilenmediğini fark edebilmesidir. Ayrıca, uygulama, araç ipi yükseltilmediği için asla kullanılmayabilecek verilerin anlık görüntülerini hesaplayan çok fazla kaynak harcar. Yani, benim sorum yerine bazı iplik "ElapsedTime" güncelleme sahip olduğuBir ipucunu ciltleme okumak için nasıl zorlanır

public class ViewModel : INotifyPropertyChanged 
{ 
    public ViewModel() 
    { 
     StartTime = DateTime.Now; 
    } 

    public DateTime StartTime { get; set; } 

    public double ElapsedTime { get { return (DateTime.Now - StartTime).TotalSeconds; } } 

    protected void RaisePropertyChanged(string prop) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

mülkiyet: ViewModel bir ile

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:l="clr-namespace:WpfApplication2" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <l:ViewModel /> 
    </Window.DataContext> 
    <Grid> 
     <Button Content="Test" 
       ToolTip="{Binding ElapsedTime}" /> 
    </Grid> 
</Window> 

:

Bu

noktayı göstermek için bir örnek uygulama Kullanıcının bir araç ipucuna ihtiyacı olması durumunda her saniye, XAML'de, ciltleme için gerekli değerin gerekli olup olmadığını söylemenin bir yolu var mı?

+0

Aşağıdaki yanıtıma bakın. İçinde herhangi bir sorunla karşılaşırsanız bana bildirin. – Gopichandar

cevap

1

iyi yolu MouseEnter ekli Davranışı sahip olacaktır senin DataContext gibi reset ve fareyi üzerinde ElapsedTime güncellemek için.

ViewModel.cs

public class ViewModel : INotifyPropertyChanged 
{ 
    public ViewModel() 
    { 
     StartTime = DateTime.Now; 
     UpdateTime = new RelayCommand(UpdateElapsedTime, new Func<bool>(() => { return true; })); 
    } 

    public DateTime StartTime { get; set; } 

    private double _elapsedTime; 

    public double ElapsedTime 
    { 
     get { return _elapsedTime; } 
     set { _elapsedTime = value; RaisePropertyChanged("ElapsedTime"); } 
    } 

    public ICommand UpdateTime { get; set; } 

    private void UpdateElapsedTime() 
    { 
     ElapsedTime = (DateTime.Now - StartTime).TotalSeconds; 
    } 

    protected void RaisePropertyChanged(string prop) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

MouseEnterBehavior.cs

public static class MouseEnterBehavior 
{ 
    public static readonly DependencyProperty MouseEnterProperty = 
     DependencyProperty.RegisterAttached("MouseEnter", 
              typeof(ICommand), 
              typeof(MouseEnterBehavior), 
              new PropertyMetadata(null, MouseEnterChanged)); 

    public static ICommand GetMouseEnter(DependencyObject obj) 
    { 
     return (ICommand)obj.GetValue(MouseEnterProperty); 
    } 

    public static void SetMouseEnter(DependencyObject obj, ICommand value) 
    { 
     obj.SetValue(MouseEnterProperty, value); 
    } 

    private static void MouseEnterChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
     UIElement uiElement = obj as UIElement; 

     if (uiElement != null) 
      uiElement.MouseEnter += new MouseEventHandler(uiElement_MouseEnter); 
    } 

    static void uiElement_MouseEnter(object sender, MouseEventArgs e) 
    { 
     UIElement uiElement = sender as UIElement; 
     if (uiElement != null) 
     { 
      ICommand command = GetMouseEnter(uiElement); 
      command.Execute(uiElement); 
     } 
    } 
} 

xaml

<Button Content="Test" 
      ToolTip="{Binding ElapsedTime}" local:MouseEnterBehavior.MouseEnter ="{Binding UpdateTime}"/> 

MouseEnterBehavior Başvurusu:How can I execute a command binding on MouseEnter of a StackPanel in WPF?

0

Değişikliği belirten bir olay tetikleyin.

Sap bu olay

SomeControl.DataContext = null; 
SomeControl.DataContext = oldObject; // re-assign your object again 
+0

Eğer bu probleminizi çözmezse, daha fazlasını tanımlayın. – AnjumSKhan

+1

Bana göre Gopichandar'ın davranışı daha uygun bir çözüm gibi görünüyor –

İlgili konular