0

i just create a GridControl from devexpress and all cell programmatically and bind its ItemsSource in this way

var gridControlAzmayesh = new GridControl
            {
                View = tv,
                ItemsSource = new CollectionViewSource
                {
                    Source = list// include some column : id,name,last name 
                }.View
            };

now i want to put a button in a column and bind it by id and when click in button open a user control whit corresponding row id but its not working my code is:

var template = new DataTemplate();
        var buttonFactory = new FrameworkElementFactory(typeof(Button)) ;
        buttonFactory.SetValue(Button.ContentProperty,"....");
        buttonFactory.SetBinding(Button.TagProperty, //add id to tag
            new Binding()
            {
                XPath = "Id", // not binding work 
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            });
        buttonFactory.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler((sender, args) =>
        {
            var aa = ((Button)sender).Tag; // read tag
            var uc = new UcEditAzmayeshSabeghe((int) aa); //  initialize a user control to open whit row id 
            UcPopupSabeghe.Child = uc;
            UcPopupSabeghe.Placement = PlacementMode.Mouse;
            UcPopupSabeghe.StaysOpen = false;
            UcPopupSabeghe.AllowsTransparency = true;
            UcPopupSabeghe.IsOpen = true;
        }));
        template.VisualTree = buttonFactory;
        gridControlAzmayesh.Columns.Add(new GridColumn
        {
            FieldName = "Id",
            Header = "...",
            CellTemplate = template,
            Width = new GridColumnWidth(40, GridColumnUnitType.Pixel)
        });
        gridControlAzmayesh.View = new TableView() { UseLightweightTemplates = UseLightweightTemplates.Row };

i cant create my gridControl in XAML because i create many gridControl with different Columns in many different Tab :Why are you so afraid of XAML i know, but XAML doesn't flexible enough :Sooo much!

exactly "id" not binding into a button i want to get every row id and bind it to a button tag properties.

sadati
  • 115
  • 1
  • 1
  • 9
  • What grid control are you using? What is your expected end result (appearance). Why don't you use a `GridView`? More details, please. Your problem should be reproducible. At least understandable for those who don't know details about your project or task. – BionicCode Dec 28 '19 at 10:30
  • @BionicCode i added some detail. – sadati Dec 28 '19 at 11:08
  • As I told you before the binding is not working as you are setting it up wrong. Use `Binding.Path` instead of `Binding.XPath`. See my answer for a recommended clean and simple solution. Never create a `DataTemplate` using C#. It's a suicidal programming style. – BionicCode Dec 28 '19 at 11:11
  • And please believe me, I don't want to offend you, but when you are saying XAML is not flexible enough, then I assume that you are simply lacking knowledge. It gives all the freedom to design the UI. It doesn't matter if each column looks different. What you are doing in C# is exactly achievable using XAML only. My answer is doing what you did but using XAML only. Now tell me which version reads better and looks simpler. XAML offers a lot of shortcuts to achieve results. E.g. XAML doesn't need the complicated `FrameworkElementFactory`. It's all hidden away from the user. – BionicCode Dec 28 '19 at 11:22
  • You have to acknowledge this. Just count lines of my `DataTemplate` markup declaration vs your C# version. – BionicCode Dec 28 '19 at 11:23
  • @BionicCode thank but let me test your code, i believe you. i'll replay your answer soon – sadati Dec 28 '19 at 11:27
  • Alright, test it. This may not be exactly what you need as I don't know your exact requirement. You never told anybody. It's an example deliberately kept simple. You can build upon it for sure. – BionicCode Dec 28 '19 at 11:30

1 Answers1

1

This is a quick but clean example to show how to create a grid view with columns, where one column hosts a ToggleButton which will open a Popup once clicked:

DataItem.cs

// The data model for the ListView
public class DataItem
{
  public DataItem(int id)
  {
    this.Id = id;
  }
  public int Id { get; set; }
}

ViewModel.cs

// The data source for the view
class ViewModel
{
  // Binding source for the ListView.ItemsSource
  public ObservableCollection<DataItem> DataItems { get; set; }
  
  public ViewModel()
  {
    this.DataItems = new ObservableCollection<DataItem>() 
    {
      new DataItem(111), 
      new DataItem(112)
    };
  }
}

UcEditAzmayeshSabeghe.xaml.cs

// Example UserControl which will display in the opened Popup
public partial class UcEditAzmayeshSabeghe : UserControl
{
  public static readonly DependencyProperty IdProperty = DependencyProperty.Register(
    "Id",
    typeof(int),
    typeof(UcEditAzmayeshSabeghe),
    new PropertyMetadata(default(int)));

  public int Id { get => (int) GetValue(UcEditAzmayeshSabeghe.IdProperty); set => SetValue(UcEditAzmayeshSabeghe.IdProperty, value); }

  public UcEditAzmayeshSabeghe()
  {
    InitializeComponent();
  }
}

UcEditAzmayeshSabeghe.xaml

<UserControl x:Class="UcEditAzmayeshSabeghe"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="450"
             d:DesignWidth="800">
  <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UcEditAzmayeshSabeghe}, Path=Id}" />
</UserControl>

Usage

MainWindow.xaml

<Window>
  <Window.DataContext>
    <ViewModel />
  </Window.DataContext>

  <ListView ItemsSource="{Binding DataItems}">
    <ListView.View>
      <GridView>
        <GridViewColumn Header="ID"
                        DisplayMemberBinding="{Binding Id}"
                        Width="40" />
        <GridViewColumn Header="Details">
          <GridViewColumn.CellTemplate>
            <DataTemplate DataType="{x:Type DataItem}">
              <Grid>
                <ToggleButton x:Name="OpenPopupButton" Content="Show Details" />
                <Popup Placement="Mouse"
                       IsOpen="{Binding ElementName=OpenPopupButton, Path=IsChecked}"
                       StaysOpen="False"
                       AllowsTransparency="True">
                  <UcEditAzmayeshSabeghe Id="{Binding Id}" />
                </Popup>
              </Grid>
            </DataTemplate>
          </GridViewColumn.CellTemplate>
        </GridViewColumn>
      </GridView>
    </ListView.View>
  </ListView>
</Window>

Result

enter image description here

This solution is clean and a joy to look at. The XAML declarations are easy to understand and easy to maintain. UI design became much simpler and verbose.

Community
  • 1
  • 1
BionicCode
  • 1
  • 4
  • 28
  • 44