【问题标题】:Binding image path to Button Background in Windows Phone 7在 Windows Phone 7 中将图像路径绑定到按钮背景
【发布时间】:2014-04-16 07:01:20
【问题描述】:

我正在尝试将图像绑定到 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>

【问题讨论】:

    标签: c# xaml windows-phone-7 listbox


    【解决方案1】:

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

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

    我以上都OK了,那么问题一定出在你的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>
    

    【讨论】:

    • 嗨@Farhad Jabiyev ..我在那堂课上遇到错误。我将在我的问题中添加屏幕截图。然后一个疑问。请告诉我,为什么我们将这个转换器用于按钮图像?
    • 嗨.. 现在没有错误了。我已经按照你的步骤。但直到现在它还没有显示图像。 :-(
    • @Farhad.. 是的.. 我的图片在 images 文件夹中。它显示在图像框中。我认为控件不在课堂上。因为我在课堂上给出了三个消息框。它没有显示消息框。
    【解决方案2】:

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

    这是我处理的一个示例。它对我来说很好。接口定义:

    <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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多