将图标添加到WPF DecExpress网格中的单元格

时间:2018-05-23 10:45:00

标签: wpf mvvm icons devexpress

我需要一双新鲜的眼睛,因为我做错了什么,弄不清楚是什么。我正在尝试向DevExpress网格添加一个图标。该图标将是可点击的,以打开调查进行编辑。图标未显示。

在SurveyVMO中:

  public Image EditIcon { get; set; }
    public static List<VMOSurvey> PopulateSurveyList()
    {
        List<Survey> surveyList = Survey.PopulateSurveyList();
        List<VMOSurvey> surveys = new List<VMOSurvey>();
        foreach (Survey svy in surveyList)
        {
            VMOSurvey s = new VMOSurvey(svy);

            if (svy.IsEditable)
                s.EditIcon =  Image.FromFile(@".\Images\pencil.png");
            surveys.Add(s);
        }
        return surveys;
    }

然后在ManageSurveyWindow.xaml:

<Window.Resources>
    <Style x:Key="SurveyGrid" TargetType="{x:Type dxg:GridControl}">
        <Setter Property="AllowLiveDataShaping" Value="True"/>
        <Setter Property="SelectionMode" Value="Row"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="FontWeight" Value="Thin"/>
        <Setter Property="FontFamily" Value="Helvetica"/>
        <Setter Property="Margin" Value="10, 10, 10, 10"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
    </Style>
    <Style x:Key="ColumnHeaderStyling" TargetType="TextBlock">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontFamily" Value="Helvetica"/>
        <Setter Property="FontWeight" Value="Thin"/>
        <Setter Property="FontSize" Value="24"/>
    </Style>
    <Style x:Key="ManageSurveyTableStyling" TargetType="{x:Type dxg:TableView}">
        <Setter Property="ShowFixedTotalSummary" Value="True"/>
        <Setter Property="IsTotalSummaryMenuEnabled" Value="False"/>
        <Setter Property="ShowGroupedColumns" Value="False" />
        <Setter Property="ShowGroupPanel" Value="False"/>
        <Setter Property="EnableImmediatePosting" Value="True"/>
        <Setter Property="AutoWidth" Value="True"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalScrollbarVisibility" Value="Auto"/>
    </Style>
</Window.Resources>
<Grid>
    <dxg:GridControl AllowLiveDataShaping="True" ItemsSource="{Binding Surveys}" >
        <dxg:GridControl.Columns>
<dxg:GridColumn x:Name="EditIconColumn">
<dxg:GridColumn.HeaderTemplate>
  <DataTemplate>
   <TextBlock Text=" " Style="{StaticResource ColumnHeaderStyling}"/>
                    </DataTemplate>
                </dxg:GridColumn.HeaderTemplate>
                <dxg:GridColumn.HeaderStyle>
                    <Style TargetType="Control">
                        <Setter Property="Background" Value="#52658F"/>
                    </Style>
                </dxg:GridColumn.HeaderStyle>
                <DataTemplate>
                    <Image Height="32" Width="32" Source="{Binding EditIcon}" />
                </DataTemplate>
            </dxg:GridColumn>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView AllowEditing="False" Style="{StaticResource ManageSurveyTableStyling}"/>
        </dxg:GridControl.View>
    </dxg:GridControl>

</Grid>

1 个答案:

答案 0 :(得分:0)

我确实搞清楚了。我的EditIcon需要是一个ImageSource:

public ImageSource EditIcon { get; set; }

然后我必须将以下代码添加到我的xaml文件中:

<dxg:GridColumn.EditSettings >
                    <dxe:ImageEditSettings MaxWidth="30">                        
</dxe:ImageEditSettings>

现在我想弄清楚如何向图片添加点击事件。 :)