C#中的DataBinding图像源?

时间:2015-01-19 23:47:49

标签: c# xaml windows-phone-8 data-binding

我的应用程序有一个名为Button1的按钮。我使用混合创建了一个样式..

<Button x:Name="Button1" Width="150" Height="150" BorderBrush="Transparent" Foreground="Transparent" Margin="151.75,0,152,90" Grid.Row="1" VerticalAlignment="Bottom" Style="{StaticResource ButtonStyle4}"/>

以下是代码:

 <Style x:Key="ButtonStyle4" TargetType="Button">
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush Stretch="Fill" ImageSource=""/>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="Padding" Value="10,5,10,6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Margin="12,9,4,15" Width="110" Height="110" >
                        <Image Source="{Binding imgSource}"/>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="Black"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="PressedHighlightBackground1">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <ImageBrush Stretch="Fill">
                                                        <ImageBrush.RelativeTransform>
                                                            <CompositeTransform CenterY="0.5" CenterX="0.5" ScaleX="0"/>
                                                        </ImageBrush.RelativeTransform>
                                                    </ImageBrush>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Background)" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <ImageBrush Stretch="Fill"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                            <Border x:Name="PressedHighlightBackground1" Background="Transparent">
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在顶部附近,您会注意到图像控件。我之前手动将它设置为图像。现在我想数据绑定图像源。不幸的是我不知道在哪里/怎么做。

1 个答案:

答案 0 :(得分:0)

您的代码是正确的。你现在唯一要做的就是在你的类中添加一个名为imgSource的DependencyProperty(参见按钮样式的第17行)并设置: this.DataContext = this;

public partial class MainWindow : Window
{
    public string imgSource
    {
        get { return (string)GetValue(imgSourceProperty); }
        set { SetValue(imgSourceProperty, value); }
    }

    // Using a DependencyProperty as the backing store for imgSource.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty imgSourceProperty =
        DependencyProperty.Register("imgSource", typeof(string), typeof(MainWindow));


    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this; 
        imgSource=@"your_image_url";
    }
}

或者在视图模型中添加属性。