C#中的数据绑定图像源?

C#中的数据绑定图像源?,c#,xaml,windows-phone-8,data-binding,C#,Xaml,Windows Phone 8,Data Binding,我的应用程序有一个名为Button1的按钮。我用blend为它创建了一个样式 <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}"/>

我的应用程序有一个名为Button1的按钮。我用blend为它创建了一个样式

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


在顶部附近,您注意到一个图像控件。我以前手动将其设置为图像。现在我想对图像源进行数据绑定。不幸的是,我不知道在哪里/怎样做

您的代码是正确的。现在唯一需要做的就是在类中添加一个名为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";
    }
}

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

在您的示例中,该属性似乎已经绑定。你能解释一下你的困惑吗?嗨,Frannseca,在尝试实现你的解决方案后,我收到了一个错误,因为“寄存器”有4个参数,而不是3个。最后一个参数必须是元数据。你能帮我吗?另外,我忘了提到我正在使用Widnows Phone尝试以下内容:公共静态只读DependencyProperty imgSourceProperty=DependencyProperty.Register(“imgSource”、typeof(string)、typeof(MainWindow)、new PropertyMetadata());