简体   繁体   English

WPF以字符串形式到达DataGridTextColumn的绑定信息

[英]WPF Reach to DataGridTextColumn's Binding info as string

I'm new at WPF. 我是WPF的新手。 I have stuck somewhere in XAML. 我被卡在XAML中。 This XAML code is working: 此XAML代码正在运行:

<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. 我想从第一行的绑定信息中获取变量字符串,例如“ All”,“ Name”,以在Image Tooltip绑定中使用。 As you can see, the binding info at first line is "Model.Name" here. 如您所见,第一行的绑定信息在此处为“ Model.Name”。 I can get just "Name" string using Converter but I can't reach that binding info. 我只能使用Converter获得“名称”字符串,但无法获取该绑定信息。

When I searched, I found a suggestion to use MultiBinding and I wrote this code: 当我搜索时,发现了使用MultiBinding的建议,并编写了以下代码:

<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 顺便说一句,DataContext.Model.Error是一个像字典类一样工作的类,而DictionaryItemConverter正在从此类中获取价值

As a result, my problem is this. 结果,我的问题是这个。

How can I reach DataTextColumn's Binding info as string? 如何以字符串形式获取DataTextColumn的绑定信息?

Thank you in advance for all answers. 预先感谢您的所有答复。

PS: English is not my first language. PS:英语不是我的母语。 If I made a mistake, sorry. 如果我弄错了,对不起。

PS: I have created a sample to show it what I want. PS:我创建了一个样本来展示我想要的东西。 You can download it here: https://mega.nz/#!oIJXgLxb!eBNqOIdby0UgkKgfqgCOVqYE1O-KwQH7cfEQxk6aCd0 您可以在这里下载: https : //mega.nz/#!oIJXgLxb!eBNqOIdby0UgkKgfqgCOVqYE1O-KwQH7cfEQxk6aCd0

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 . 然后转到当前单元格,然后在DictionaryItemConverter查找DataContext属性的Binding

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. 也许您可以修改问题以显示总体目标以及ViewModel涉及的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM