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