【问题标题】:How to assign Dynamic Resource to a button in WPF code at run time如何在运行时将动态资源分配给 WPF 代码中的按钮
【发布时间】:2014-09-15 21:39:48
【问题描述】:

我在使用从现有资源创建的视觉画笔填充矩形时遇到了以下问题。

如果我要在 XAML 中对其进行硬编码,它将看起来像这样......

<ToggleButton Style="{DynamicResource MetroCircleToggleButtonStyle}" Height="60" Width="60">
<ToggleButton.Content>
<StackPanel Orientation="Vertical">
       <Rectangle Height="30" Width="30">
        <Rectangle.Fill>
        <VisualBrush Visual="{StaticResource appbar_database}" Stretch="Uniform" />
        </Rectangle.Fill>
       </Rectangle>
 </StackPanel>
 </ToggleButton.Content>

我试图在后面的代码中执行此操作...并在运行时将资源名称作为组合控件(按钮和标签)的一部分传递。 我为这个组合控件(标题和图标资源)创建了两个 DependencyProperty,并尝试在更新图标资源后更新按钮内容,但是这部分代码似乎从未被执行:( ...有什么想法吗?

[编辑] 我已经为图标资源路径创建了依赖属性...

public string Caption
{
    get { return (string)GetValue(TextProperty); }
    set
    {
        SetValue(TextProperty, value);

        if (string.IsNullOrEmpty(value))
        {
            tbCaption.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}

    public string IconResource
    {
        get { return (string)GetValue(IconProperty); }
        set
        {
            SetValue(IconProperty, value);

            object icon = TryFindResource(value);
            if (icon != null)
            {
                Visual iconVisual = icon as Visual;
                ((Rectangle)mainButton.Content).Fill = new VisualBrush(iconVisual);
            }
        }
    }

public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Caption", typeof(string), typeof(ButtonWithText), null);

public static readonly DependencyProperty IconProperty =
    DependencyProperty.Register("IconResource", typeof(string), typeof(ButtonWithText), null)

;

有没有办法在 XAML 中绑定包含动态资源名称的变量?

谢谢!

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    免责声明:我离开了一会儿,您对您的帖子进行了编辑,这没有错! :) 我希望这些信息对你仍然有用。您可以更简单获取您的资源。

    如果它是一个共享资源,你可以做一个(比如在 app.xaml 中,或者一个合并的字典。那里)。我只是做了一个小示例应用程序并将样式放在 app.xaml 中。

    var style = (Style)Application.Current.TryFindResource("MetroCircleToggleButtonStyle");
    

    在代码隐藏中:

    var style = (Style)TryFindResource("MetroCircleToggleButtonStyle");
    

    在哪里获取它取决于您将资源放在哪里。 我没有找到适合您的范围链接,但如果您愿意,可以在 here 附近挖掘一下。 我使用 TDD,所以我会为此提供服务,特别是如果你必须在那里使用 Applicationl.Current,它是静态的等。实际上我会将它作为我的 viewmodelbase 某处的接口,无论如何那是 Offtopic。希望这对你有帮助! :)

    FrameworkElement.TryFindResource

    Application.TryfindResource

    干杯,

    斯蒂安

    【讨论】:

    • 是的,这可以工作 - 但资源值必须从不同的角度传入,这就是为什么我必须创建依赖属性并让资源以这种方式设置。
    【解决方案2】:

    好的,我想我找到了解决这个问题的方法。

    我能够通过执行以下操作传入资源:

    在后面的代码中...

    public Canvas IconResource
            {
                get { return (Canvas)GetValue(IconProperty); }
                set
                {
                    SetValue(IconProperty, value);
                }
            }
    
            public static readonly DependencyProperty IconProperty =
                DependencyProperty.Register("IconResource", typeof(Canvas), typeof(ButtonWithText), null);
    

    在 XAML 中

     <ToggleButton x:Name="mainButton" Grid.Row="0" Style="{DynamicResource MetroCircleToggleButtonStyle}" Height="60" Width="60">
        <ToggleButton.Content>
            <Rectangle Height="30" Width="30">
                    <Rectangle.Fill>
                        <VisualBrush Visual="{Binding IconResource}"></VisualBrush>
                    </Rectangle.Fill>
                </Rectangle>
            </ToggleButton.Content>
    </ToggleButton>
    

    并且...当尝试使用此控件时,我将传入整个动态资源

    <k:ButtonWithText Caption="Hello" IconResource="{StaticResource appbar_magnify}"></k:ButtonWithText>
    

    【讨论】:

      【解决方案3】:

      型号:

      public class Status
      {
          public string IconName{get;set;}
          public Canvas Icon 
          {
              get 
              {
                  try
                  {
                      return Application.Current.FindResource(IconName) as Canvas;
      
                  }                
                  catch(System.Windows.ResourceReferenceKeyNotFoundException e)
                  {
                      return null;
                  }
              }
          }
      }
      

      视图模型:

      public StatusViewModel
      {
          public Status Status{get;set;}
          public StatusViewModel()
          {
              Status = new Status{IconName="appbar_database}"};
          }
      }
      

      页面.cs

      DataContext = new StatusViewModel();
      

      页面 XAML

      <ToggleButton x:Name="mainButton" Grid.Row="0" Style="{DynamicResource MetroCircleToggleButtonStyle}" Height="60" Width="60">
      <ToggleButton.Content>
          <Rectangle Height="30" Width="30">
                  <Rectangle.Fill>
                      <VisualBrush Visual="{Binding Status.Icon}"></VisualBrush>
                  </Rectangle.Fill>
              </Rectangle>
          </ToggleButton.Content>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-30
        • 2010-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多