I
I
iXelper2020-11-28 13:38:05
WPF
iXelper, 2020-11-28 13:38:05

How to interact with element inside DataTemplate?

There is a DataGrid, it has a column with Ellipse for visual convenience)
Actually, the question itself is how to change the Ellipse color for the selected cell?

<DataGrid x:Name="infoDG"
                 Background="Gray"
                 AutoGenerateColumns="False"
                 CanUserAddRows="False">
        <DataGrid.Columns>
                    
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Ellipse x:Name="checkIdEll"
                                         Fill="Green"
                                         Width="10"
                                         Height="10"
                                         VerticalAlignment="Center"
                                         HorizontalAlignment="Center">
                                </Ellipse>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                  
      </DataGrid.Columns>
</DataGrid>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2020-11-29
@iXelper

You can also make your own UserControl, in which to place Ellipse and all the logic for modifying its appearance in XAML. But is it necessary? I think no. There are many ways to do this.

<DataGrid
    AutoGenerateColumns="False"
    ItemsSource="{Binding Income}">
    <DataGrid.Columns>
        <DataGridTemplateColumn
            CanUserReorder="False"
            CanUserResize="False"
            CanUserSort="False"
            IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate DataType="viewModels:IncomeViewModel">
                    <Ellipse Fill="{Binding IsModified, Converter={StaticResource BooleanToBrushConverter}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
</DataGrid>

using GalaSoft.MvvmLight;

namespace WpfApp.ViewModels
{
    public class IncomeViewModel : ViewModelBase
    {
        private bool _isModified;

        public bool IsModified
        {
            get { return _isModified; }
            set { Set(ref _isModified, value); }
        }
    }
}

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApp.Converters
{
    [ValueConversion(typeof(bool), typeof(SolidColorBrush))]
    public class BooleanToBrushConverter : IValueConverter
    {
        public SolidColorBrush TrueBrush { get; set; } = new SolidColorBrush(Colors.Tomato);

        public SolidColorBrush FalseBrush { get; set; } = new SolidColorBrush(Colors.CornflowerBlue);

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? TrueBrush : FalseBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question