4

In WPF, is it possible to bind the key in "{StaticResource key}"to a variable.

For example. I have a variable ExecutionState with the states Active and Completed.

In my ResourceDictionary I have

<Style TargetType="{x:Type TextBlock}" x:Key="Active">
        <Setter Property="Foreground" Value="Yellow"/>
    </Style>
    <Style TargetType="{x:Type TextBlock}" x:Key="Completed">
        <Setter Property="Foreground" Value="Green"/>
    </Style>

Instead of having

<TextBlock Style="{StaticResource Active}"/>

I Would like to have something like

<TextBlock Style="{StaticResource {Binding ExecutionState}}"/>

Thus if the state changes the text color changes. Is something like this even possible? I can achieve the wanted functionality using triggers, but I have to reuse it at several places and I don't want to clutter my code. I am using MVVM also.

thanx


Redirecting to an external URL in a new tab and performing an action in backing bean at the same time

I'm working at a jsf application that at a certain time need to open an external page in a new tab, leaving the first page active. I need to find a way to make the application perform, in a single button click:

  1. a redirect to an external URL in a new tab
  2. an action which disables the button itself in the original page

I've tried using an <outputLink /> but it has no action attribute. I've tried using a <commandLink />but it's unable to redirect outside. I've also tried a <commandLink /> with target="_blank" and a redirection coded in the backing bean:

<h:commandLink onclick="submit();" target="_blank" action="#{myBean.execute}" disabled="#{myBean.linkDisabled}" value="external link" />

and, in the backing bean:

public String execute() {
    linkDisabled = true;

    try{
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        externalContext.redirect(Constants.EXTERNAL_URL);
    } catch(Exception e){
        throw new FacesException("Redirection failed");
    }

    return THIS_PAGE;
}

A page is opened in a new tab but it's the current page instead of the that with URL Constants.EXTERNAL_URLand the button is still enabled. No error message is shown. Any suggestion?

Thanks in advance, Andrea

4

2 に答える 2

0

いいえ、できません。バインディングは、DependencyPropertyにのみ設定できます。StaticResourceはDependencyObjectではないため、DependencyPropertyはありません。トリガーを使用するか、独自のアタッチされた動作を開発する必要があります。

于 2012-04-17T10:57:37.107 に答える
0

を達成する直接的な方法はありません。添付プロパティを 1 つ作成し、バインドするプロパティ名を割り当てます。プロパティ変更コールバック関数で、コントロール スタイルを更新します。

<TextBlock  dep:CustomStyle.StyleName="{Binding ExecutionState}"    Text="Thiru"        />


public static  class CustomStyle
{
    static FrameworkPropertyMetadata _styleMetadata = new FrameworkPropertyMetadata(
                                     string.Empty, FrameworkPropertyMetadataOptions.AffectsRender, StyleNamePropertyChangeCallBack);

    public static readonly DependencyProperty StyleNameProperty =
        DependencyProperty.RegisterAttached("StyleName", typeof (String), typeof (CustomStyle), _styleMetadata);

    public static void SetStyleName(UIElement element, string value)
    {
        element.SetValue(StyleNameProperty, value);
    }
    public static Boolean GetStyleName(UIElement element)
    {
        return (Boolean)element.GetValue(StyleNameProperty);
    }


    public static void StyleNamePropertyChangeCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

        FrameworkElement ctrl = d as FrameworkElement;

        if (ctrl.IsLoaded)
        {

            string styleName = Convert.ToString(e.NewValue);
            if (!string.IsNullOrEmpty(styleName))
            {
                ctrl.Style = ctrl.TryFindResource(styleName) as Style;
            }
        }
    }
}
于 2012-04-17T11:17:12.883 に答える