2013-01-03 84 views
0

我在比較XAML中登錄用戶名的名稱。我在代碼隱藏中將其設置爲"System.Threading.Thread.CurrentPrincipal.Identity.Name",我試圖將其設置爲DataTrigger中的值,但Visual Studio告訴我,我不能在DataTrigger值中使用Binding值,只能在DependencyPropertyDependencyObject中使用。如何將登錄的用戶名與DataTrigger進行比較

我試過一千種方法,但我總是得到相同的錯誤。任何想法?

public string usuarioactual; 

public Amigos() 
{ 
    InitializeComponent(); 
    presenter = new PresenterAmigos(this); 
    presenter.ObtenerAmistades(); 
    presenter.ObtenerUsuarioActual(); 
    usuarioactual = System.Threading.Thread.CurrentPrincipal.Identity.Name; 

} 


<Style.Triggers> 
    <DataTrigger Binding="{Binding Recibida.Email}" Value="{Binding usuarioactual}"> 
     <Setter Property="Content" Value="{Binding Enviada.Email}"/> 
    </DataTrigger> 
    <DataTrigger Binding="{Binding Enviada.Email}" Value="{Binding usuarioactual}"> 
     <Setter Property="Content" Value="{Binding Recibida.Email}"/> 
    </DataTrigger> 
</Style.Triggers> 

回答

0

您可以使用BindingConverter計算結果爲true/falseDataTrigger使用此true/false作爲值條件​​。

public class UserNameToBooleanConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var username = (string)value; 
     if (username == "System.Threading.Thread.CurrentPrincipal.Identity.Name") 
      return true; 
     return false; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
0

你可以只讓你usuarioactual變量DependencyProperty

public Amigos() 
{ 
    InitializeComponent(); 
    presenter = new PresenterAmigos(this); 
    presenter.ObtenerAmistades(); 
    presenter.ObtenerUsuarioActual(); 
    usuarioactual = System.Threading.Thread.CurrentPrincipal.Identity.Name; 
} 

public string usuarioactual 
{ 
    get { return (string)GetValue(usuarioactualProperty); } 
    set { SetValue(usuarioactualProperty, value); } 
} 

// Using a DependencyProperty as the backing store for usuarioactual. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty usuarioactualProperty = 
    DependencyProperty.Register("usuarioactual", typeof(string), typeof(Amigos), new UIPropertyMetadata(string.Empty)); 
相關問題