【问题标题】:XAML to C# Code: Binding Image to ListViewXAML 到 C# 代码:将图像绑定到 ListView
【发布时间】:2018-02-07 13:50:44
【问题描述】:

我正在尝试通过后面的代码将图像绑定到ListView。目标是用 C# 编写这部分代码。代码中的绑定如何工作? 这是我的 xaml 代码:

<ListView x:FieldModifier="public" x:Name="MyListView" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding ListViewDataList}">
    <ListView.View>
        <GridView x:Uid="GridViewTest">
            <GridViewColumn Header="Column1" DisplayMemberBinding="{Binding Column1Text}"/>
            <GridViewColumn Header="Column2">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="50" Height="50" Source="{Binding Column2Img}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Column1TextColumn2Img 是属性。 我尝试过这样的事情:

GridView MyGridView = new GridView();

GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("Column1Text");
gvc1.Header = "Column1";
MyGridView.Columns.Add(gvc1);

System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Height = 50;
img.Width = 50;
img.Source = new Binding("Column2Img");

FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
DataTemplate dt = new DataTemplate();
dt.VisualTree = spFactory;

GridViewColumn gvc2 = new GridViewColumn();
gvc2.Header = "Column2";
gvc2.CellTemplate = dt;
MyGridView.Columns.Add(gvc2);

MyListView.View = MyGridView;

但是后面代码中的图像绑定不起作用。

【问题讨论】:

  • 在您的绑定上使用转换器,您应该没问题。但你的形象是什么?字符串、字节[]、图像源、位图? byte example.
  • @XAMlMAX 图像是字符串类型。

标签: c# wpf xaml listview


【解决方案1】:

您应该绑定Source 属性,而不是直接将其设置为Binding 对象:

img.SetBinding(Image.SourceProperty, new Binding("Column2Img"));

但您还需要为添加到spFactoryImage 元素创建一个FrameworkElementFactory

FrameworkElementFactory imgFactory = new FrameworkElementFactory(typeof(Image));
imgFactory.SetValue(Image.HeightProperty, 50.0);
imgFactory.SetValue(Image.WidthProperty, 50.0);
imgFactory.SetBinding(Image.SourceProperty, new Binding("Column2Img"));

FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
spFactory.AppendChild(imgFactory);

请注意,使用FrameworkElementFactory 是一种以编程方式创建模板的已弃用方式:https://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx

以编程方式创建模板的推荐方法是使用XamlReader 类的Load 方法从string 或内存流加载XAML。

【讨论】:

  • 谢谢!我遇到了一个异常:“50”不是 imgFactory.SetValue(Image.HeightProperty, 50) 中属性“Height”的有效值;
  • 将其更改为 50.0 或 50d 以表明它是一个双精度值。我编辑了我的答案。
  • 使用 xaml 代码的解决方案按预期显示列表视图中的图像。但不是后面有代码的那个。我在窗口的加载事件中使用 c# 创建列表视图。第一列包含字符串,如预期的那样,但不是第二列。只有绑定值的类型才能看成字符串。
  • 好的解决了。我忘记了“gvc2.CellTemplate = dt;”这一行
猜你喜欢
  • 1970-01-01
  • 2016-10-03
  • 2016-03-29
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2012-05-14
相关资源
最近更新 更多