2015-10-27 84 views
-2

我很好奇最好的做法是如何處理與標籤位於相同名稱空間和窗口內的類中的標籤屬性。訪問公共類中的標籤

示例代碼:

namespace SampleApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public class labelController 
     { 
      public void setText() 
      { 
       //How do I reference label1 from here properly/in best practice to modify it's content? 
      } 
     } 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     labelController lblCtrl = new labelController(); 

     private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      label1.Content = "Cows Go Moo!"; 
     } 
    } 
} 

我將如何引用標籤,或任何工具箱項從labelController的功能的setText?

+4

「正確」的方法是使用MVC或MVVM和數據綁定標籤到mod埃爾。 –

+0

@RonBeyer我們假設我將標籤綁定到一個變量,然後我有一個函數檢查變量,並希望根據變量的值將背景更改爲特定的顏色。如何更新/如何更新該標籤的背景顏色?那麼我是否有必要從函數中訪問標籤,因爲我已經提供了一個示例?對不起,如果我的問題很愚蠢,我對.NET更新,並試圖理解一些概念。 –

+1

不,在這種情況下,您可以使用屬性綁定甚至動畫來監視綁定的模型字段,並更改您希望的所有屬性,全部來自XAML而不觸及代碼隱藏。 –

回答

0

1.處理標籤或任何其他UI元素屬性的最佳方法是對WPF使用數據綁定或MVVM模式。

例如,你可以進行數據綁定您的標籤屬性的一些成員屬性在背後類代碼(即Window類),如下:

<Label Content="{Binding LabelContent}" /> 

凡LabelContent在身後,下面類代碼中的成員:

private string _LabelContent; 
    public string LabelContent 
    { 
     get { return _LabelContent; } 
     set 
     { 
      _LabelContent= value; 
      RaisePropertyChangedEvent("LabelContent"); 
     } 
    } 

或者你可以操縱不同的類之外的其他標籤屬性(視圖模型)比你的代碼隱藏類,並在窗口代碼隱藏設置的DataContext指向下面的視圖模型類:

public class ViewModel : INotiFyPropertyChanged 
{ 
    private string _LabelContent; 
    public string LabelContent 
    { 
     get { return _LabelContent; } 
     set 
     { 
      _LabelContent= value; 
      RaisePropertyChangedEvent("LabelContent"); 
     } 
     } 
    } 

//setting datacontext is crucial in MVVM 
public partial class MainWindow : Window 
{ 
    public ViewModel vm; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new ViewModel(); 
} 

請記住,在這兩種情況下,您都需要在Window類以及視圖模型類上實現INotifyPropertyChanged接口。

INOtify ...的主要目的是在數據在代碼隱藏或viewmodel類中變化時,或者反之亦然(即UI中的某些TextBox變化)時,將數據推入UI。 這樣,你不必從你的codebehind/viewmodel顯式地將數據拉入或者推入UI。

例如,你的ButtonClick方法可以更改如下:

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     LabelContent = "Cows Go Moo!"; 
    } 

由於LabelContent勢必標籤文本屬性在XAML文件時,用戶界面會自動更改每當執行ButtonClick方法的標籤文本。使用inotify的...界面

PFB代碼:

public event PropertyChangedEventHandler PropertyChanged; 
    protected void RaisePropertyChangedEvent(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      var e = new PropertyChangedEventArgs(propertyName); 
      this.PropertyChanged(this, e); 
     } 
    } 

你可以找到MVVM,WPF數據綁定以及inotify的詳細信息..接口在MSDN網站。

  1. 如果要根據另一個屬性say,Content來更改標籤的屬性,則使用實現IConverter接口的類根據另一個屬性動態更改屬性。例如,

    public class StringToColorConverter: IValueConverter 
    { 
    
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
    string contentvalue=value.ToString(); 
        var result = (contentvalue.Equals("something")) ? Brushes.Green : Brushes.Red; 
        return result; 
    } 
    
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
    
    
    } 
    

您可以使用轉換器基於如下的XAML內容屬性來改變你的背景屬性:

   <Window.Resources> 
       <StringToColorConverter x:key="stringtocolorconverter"/> 
        </Window.Resources> 

     <Label content="{Binding LabelContent}" Background="{Binding LabelContent, Converter={StaticResource stringtocolorconverter}}" /> 

或給標籤的名稱和使用轉換背景屬性標籤的內容屬性如下:

  <Label Name="LabelElement" content="{Binding LabelContent}" Background="{Binding Path=content, ElementName="LabelElement",Converter={StaticResource stringtocolorconverter}}" />