繁体   English   中英

在Windows Phone 7中将图像路径绑定到按钮背景

[英]Binding image path to Button Background in Windows Phone 7

我试图将图像绑定到ViewModel的按钮。 但是我不能绑定到按钮。 但是,如果我将此相同的值绑定到imagebox,则表示它显示图像。

 <ListBox Tap="listBox1_Tap" Height="444" ItemsSource="{Binding StudentDetails,Mode=TwoWay}" HorizontalAlignment="Left" Margin="2,34,0,0" Name="listBox1" VerticalAlignment="Top" Width="476" BorderBrush="#00410D0D">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                            <StackPanel Orientation="Horizontal"> 

                                <Image Height="50" Source="{Binding addImage}" HorizontalAlignment="Left"  Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="50" />

                                <Button Height="80" Width="80" DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}">
                                    <Button.Background>
                                        <ImageBrush ImageSource="{Binding addImage}" Stretch="Fill" />
                                    </Button.Background>
                                </Button>

                            </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我假设您已将图像的生成操作设置为Resource,这是默认值。 并且您的字符串值必须是这样的:

string addImage =  "/Application;component/Images/image_name.png";

我以上所有方法都可以,那么问题一定出在您的Button 您已经将其DataContext设置为ListBox1 DataContext。 为什么? 没有必要。

更改

<Button Height="80" Width="80" DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}">
    <Button.Background>
        <ImageBrush ImageSource="{Binding addImage}" Stretch="Fill" />
    </Button.Background>
</Button>

<Button Height="80" Width="80" Command="{Binding addPerson}">
    <Button.Background>
        <ImageBrush ImageSource="{Binding addImage}" Stretch="Fill" />
    </Button.Background>
</Button>

您应该使用转换器来绑定图像。

这是我工作的样本。 它对我来说很好。 接口定义:

<DataTemplate x:Name="lstbxCreateEventTypesTiles">
    <Button x:Name="btn1" Content="{Binding Name}" CommandParameter="{Binding ID}" Click="Button_Click" 
    Style="{StaticResource ButtonStyle}" HorizontalContentAlignment="Left" BorderThickness="0" FontSize="42.67" FontFamily="Segoe WP SemiLight" Foreground="White" BorderBrush="{x:Null}">
        <Button.Background>
            <ImageBrush ImageSource="{Binding MasterTypeID, Converter={StaticResource ImageConverter}}" Stretch="None"/>
        </Button.Background>
    </Button>
</DataTemplate>

该类的来源:

公共类ImageConverter:IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        ImageSource img = null;
        try {
            if (value != null) {


                switch (value.ToString()) {
                    case "1":
                        value = "Assets/tile_bg.png";
                        break;
                    case "2":
                        value = "Assets/tile2_bg.png";
                        break;

                    default:
                        break;
                }

                BitmapImage image = new BitmapImage();
                image.SetSource(Application.GetResourceStream(new Uri(@value.ToString(), UriKind.Relative)).Stream);
                img = image;


            }
        } catch (Exception ex) {

        }
        return img;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM