Wpf 使用IValueConverter将绑定文本转换为图像

Wpf 使用IValueConverter将绑定文本转换为图像,wpf,Wpf,我有一个网格,我想从该单元格中提取文本,将其转换为图像并将其发送回网格 <Grid> <xcdg:DataGridControl Height="311" HorizontalAlignment="Left" Name="dataGridControl1" VerticalAlignment="Top"

我有一个网格,我想从该单元格中提取文本,将其转换为图像并将其发送回网格

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>
以下是我的模板代码:

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>
这是我的xaml代码:

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>

我在模板代码中遇到的问题是指定参数是什么。。。。由于数据是绑定的,我不知道如何将其发送到converter类。。请帮忙

这取决于要传递的参数类型。您可以像这样使用
converterParameter

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>
Source="{Binding Converter={StaticResource catConverter}, ConverterParameter='some parameter'}"
但是,
ConverterParameter
不是依赖项属性,因此不能使用绑定。如果要提供动态上下文,则需要使用多绑定和多值转换器,如下所示:

<Grid>
    <xcdg:DataGridControl Height="311" 
                          HorizontalAlignment="Left" 
                          Name="dataGridControl1" 
                          VerticalAlignment="Top" 
                          Width="503"
                          ItemsSource="{Binding Source={StaticResource Clients}}">
        <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name" />
            <xcdg:Column FieldName="Age" />
            <xcdg:Column FieldName="Category" />
            <xcdg:Column FieldName="Color"
                         CellContentTemplate="{StaticResource categoryCellTemplate}" />
            <xcdg:Column FieldName="DOB" />
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>
<Image.Source>
    <MultiBinding Converter={StaticResource catMultiConverter}>
      <Binding .../>
      <Binding .../>
    </MultiBinding>
</Image.Source>


基本上,我想将类别值作为参数传递给它。Category是一个整数值{1,2,3,4},Convert函数将返回一个位图图像,现在converter类很好了。这正是我需要传递的参数。不太确定如何传递参数。颜色字段也是整数值{1,2,3,4}。converter类将把int转换成一个图像。。。我想我应该在问题中提供更多的信息我有点困惑。在您的示例中,它显示了
Color
字段有一个cel ltemplate。这是否意味着您的转换器取决于颜色值和类别值?是的。颜色值与类别值相同。。。所以在我的个人课上。。我有一个财产是这样的。。。公共int颜色{get{返回this.Category;}}。。。我做了进一步的调查。。。Convert()中的参数“value”始终传递null值。这一定很奇怪,因为我编写的函数和显示图片的方式与其他程序员的方式相同。这已经变得令人沮丧了,如果您只想将单个值传递到转换器中,那么您的方法应该已经起作用了。如果传入转换器的值为null,则表示模板的DataContext设置不正确。你似乎没有使用标准的网格,所以也许问题就在那里?我能让它工作。在我的Convert()中,我返回了一个位图图像对象,但设置时遇到了问题。我最后做的就是以字符串的形式返回图片的位置,结果它成功了。。。这么多代码一无所获!