如何在运行时将动态资源分配给WPF代码中的按钮

时间:2014-09-15 21:39:48

标签: c# wpf xaml

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

如果我要在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中绑定包含动态资源名称的变量?

谢谢!

3 个答案:

答案 0 :(得分:2)

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

如果它是共享资源,你可以做一个(比如app.xaml或合并的dict。)。我刚刚制作了一个小样本应用程序并将样式放在app.xaml中。

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

在codebehind中:

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

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

<强> FrameworkElement.TryFindResource

<强> Application.TryfindResource

干杯,

了Stian

答案 1 :(得分:0)

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

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

在背后的代码中......

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>

答案 2 :(得分:0)

型号:

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}"};
    }
}

Page .cs

DataContext = new StatusViewModel();

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

相关问题