【问题标题】:How to find DataContext in Setter Value property?如何在 Setter Value 属性中找到 DataContext?
【发布时间】:2017-07-18 07:01:46
【问题描述】:

我尝试将 Tooltip 绑定到 DevExpress 网格控件中的单元格值,但是 我在 Setter.Value 属性“丢失”了 DataContext:

这里是GridControl的完整代码(只有一栏):

 <dxg:GridControl Grid.Row="0"                                       
                                 x:Name="grid"
                                 VerticalAlignment="Stretch"                                     
                                 HorizontalAlignment="Stretch"
                                 dx:ThemeManager.ThemeName="Seven"                                     
                                 ScrollViewer.CanContentScroll="True"
                                 ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                 ScrollViewer.VerticalScrollBarVisibility="Auto"                                     
                                 ItemsSource="{Binding ObjectViewModel.Collection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                 SelectedItem="{Binding CurrentElement,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,TargetNullValue=null}"                                                             
                                 >

                                <dxg:GridControl.View>

                                    <!--region #RowCellMenuCustomization-->
                                    <dxg:TableView x:Name="view" AutoWidth="True"
                                                   UseLightweightTemplates="None"
                                                   >

                                        </dxg:TableView.RowCellMenuCustomizations>
                                    </dxg:TableView>
                                    <!--endregion #RowCellMenuCustomization-->
                                </dxg:GridControl.View>

                                <dxg:GridControl.Columns>

                                    <dxg:GridColumn                                            
                            Header="Address"
                            Binding="{Binding Address,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"  
                            AllowEditing="False"                                   
                            HorizontalHeaderContentAlignment="Stretch"
                            Width="*"                                                                                            
                            AllowResizing="True" 
                            HeaderToolTip="Address"                                
                            >
                                        <dxg:GridColumn.CellStyle
                                             >
                                            <Style x:Name="toolTipStyle"
                                                   BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"                                                        
                                                   TargetType="dxg:GridCellContentPresenter">                                                                                             
                                                <Setter Property="ToolTip">
                                                    <Setter.Value>
                                                        <TextBlock Text="{Binding Path=Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                                                    </Setter.Value>
                                                </Setter>
                                            </Style>
                                        </dxg:GridColumn.CellStyle>
                                    </dxg:GridColumn>

                                </dxg:GridControl.Columns>                                                                   
                            </dxg:GridControl>

采集类代码:

public class Element
 {
     public String Address 
     {
        return SomeObject.Address;
     }
   // other properties
 }

所以,感谢this answer:它适用于一些非绑定文本,但是当我尝试将它绑定到属性时它不起作用。

Visual Studio 输出窗口日志:

System.Windows.Data Error: 40 : BindingExpression path error: 'Address' property not found on 'object' ''TextBlock' (Name='')'. BindingExpression:Path=Address; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

我好像丢失了DataContext,但是如何在Setter.Value处绑定呢?

附: @Rekshino,我这样做了,输出日志是:

System.Windows.Data Error: 40 : BindingExpression path error: 'Address' property not found on 'object' ''EditGridCellData' (HashCode=41748728)'. BindingExpression:Path=DataContext.Address; DataItem='TextBlock' (Name=''); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

【问题讨论】:

    标签: c# wpf datacontext devexpress-wpf


    【解决方案1】:

    试试这个:

    <Setter.Value>
        <TextBlock Text="{Binding RowData.Row.Address}"/>
    </Setter.Value>
    

    或者:

    <dxg:GridColumn.CellStyle>
        <Style x:Name="toolTipStyle"
                       BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=CellStyle}}"                                                        
                       TargetType="dxg:GridCellContentPresenter">
            <Setter Property="ToolTip" Value="{Binding RowData.Row.Address}"/>
        </Style>
    </dxg:GridColumn.CellStyle>
    

    【讨论】:

      【解决方案2】:

      试试

      <TextBlock Text="{Binding Path=DataContext.Address,RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
      

      你试图绑定到 TextBlock 而不是它的 DataContext

      【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      相关资源
      最近更新 更多