0

I'm new at WPF. I have stuck somewhere in XAML. This XAML code is working:

<DataGridTextColumn Binding="{Binding Model.Name, UpdateSourceTrigger=PropertyChanged}">
  <DataGridTextColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
        <DataTrigger Value="False"
                     Binding="{Binding DataContext.Model.Error[All],
                                  RelativeSource={RelativeSource FindAncestor,
                                            AncestorType={x:Type DataGridRow}},
                                  Converter={StaticResource IsNull}}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border>
                  <Grid>
                    <ContentPresenter x:Name="_cp" />
                    <Image Source="{DynamicResource Img.Warning}"
                             ToolTip="{Binding DataContext.Model.Error[Name],
                                       RelativeSource={RelativeSource FindAncestor,
                                             AncestorType={x:Type DataGridRow}}" />
                  </Grid>
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

I want to get variables strings like "All", "Name" from first line's Binding info to use in Image Tooltip Binding. As you can see, the binding info at first line is "Model.Name" here. I can get just "Name" string using Converter but I can't reach that binding info.

When I searched, I found a suggestion to use MultiBinding and I wrote this code:

<DataGridTextColumn Binding="{Binding Model.Name, UpdateSourceTrigger=PropertyChanged}">
  <DataGridTextColumn.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
        <DataTrigger Value="False">
          <DataTrigger.Binding>
            <MultiBinding>
              <MultiBinding.Converter>
                <conv:DictionaryItemConverter />
              </MultiBinding.Converter>
              <Binding Path="DataContext.Model.Error"
                       RelativeSource="{RelativeSource FindAncestor,
                                        AncestorType={x:Type DataGridRow}}" />
              <Binding Path="?????" RelativeSource="?????" />
            </MultiBinding>
          </DataTrigger.Binding>

          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border>
                  <Grid>
                    <ContentPresenter x:Name="_cp" />
                    <Image Source="{DynamicResource Img.Warning}">
                      <Image.ToolTip>
                        <Binding>
                          ?????
                        </Binding>
                      </Image.ToolTip>
                    </Image
                  </Grid>
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

But as you can see, I have stuck to get the string info. BTW, DataContext.Model.Error is a class that is working like a Dictionary Class and DictionaryItemConverter is getting value from this class

As a result, my problem is this.

How can I reach DataTextColumn's Binding info as string?

Thank you in advance for all answers.

P.S.: English is not my first language. If I made a mistake, sorry.

P.S.: I have created a sample to show it what I want. You can download it here: https://mega.nz/#!oIJXgLxb!eBNqOIdby0UgkKgfqgCOVqYE1O-KwQH7cfEQxk6aCd0

bafsar
  • 1,080
  • 2
  • 16
  • 16

1 Answers1

0

you could try this:

    <DataTrigger Value="False">
      <DataTrigger.Binding>
        <MultiBinding>
          <MultiBinding.Converter>
            <conv:DictionaryItemConverter />
          </MultiBinding.Converter>
          <Binding Path="DataContext.Model.Error"
                   RelativeSource="{RelativeSource FindAncestor,
                                    AncestorType={x:Type DataGridRow}}" />

          <Binding RelativeSource="{RelativeSource FindAncestor,
                                    AncestorType={x:Type DataGridCell}}" />
        </MultiBinding>
      </DataTrigger.Binding>

then you get to the current cell and you then look up the Binding for the DataContext property within the DictionaryItemConverter.

But really this feels like you're doing something way too complicated. Maybe you can modify your question to show your overall goal and the properties involved on the ViewModel.

Markus Hütter
  • 7,796
  • 1
  • 36
  • 63
  • Thank you Markus, I can do it with this way but this is not what I want. I want to reach property name of DataGridTextColumn's binding. Not value. The value could be anything. But I need to reach 'Model.Name', 'Model.Explanation', 'Model.All', etc. information as string. In the converter, checks DataContext.Model.Error (a class like Dictionary) has this key: string v = DataContext.Model.Error['Name']; return !string.IsNullOrWhiteSpace(v); If I can reach the property name (like 'Model.Name') as string then I can split and use it. – bafsar Jul 12 '16 at 16:07
  • so you just want a solution to not have to repeat all that same stuff with only the _All_ and _Name_ changed?? – Markus Hütter Jul 12 '16 at 16:13
  • I still can't make sense of it all, how does your ViewModel look like? Why do you have a Dictionary instead of separate properties like `AllErrors`, `NameErrors` if you have only a couple of these in the Dictionary? – Markus Hütter Jul 12 '16 at 16:17
  • Thank you for all interesting Markus. It doesn't work, still. Let me explain why I have a (class like) dictionary. I tried use IDataErrorInfo for error validation. But I encountered an error about adding new row without any information. I decided to write my own error class. This kind of dictionary class is for this. It holds any error info that I need to be. I added a sample project to my question. If you can see it, probably you will figure it out. BTW, in sample, you will see some explanation in Turkish. In MainWindow.xaml, 1.exp. is saying 'it works properly'. 2.exp. is saying 'Not'. – bafsar Jul 12 '16 at 18:23