0

我正在尝试比较 XAML 中记录的用户名的名称。我将它设置在代码隐藏中的变量中,"System.Threading.Thread.CurrentPrincipal.Identity.Name"并尝试将其设置为 DataTrigger 中的值,但 Visual Studio 告诉我不能BindingDataTriggerValue 中使用,只能在 aDependencyProperty或中使用DependencyObject

我已经尝试了一千种方法,但我总是得到同样的错误。任何想法?

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>
4

2 回答 2

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));
于 2013-01-03T22:05:34.903 回答
0

您可以使用 评估为的BindingConvertertrue/false并将其true/false用作 中的值条件DataTrigger

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();
    }
于 2013-01-03T18:23:03.977 回答