开发者

WPF: Binding to converted object failed!

开发者 https://www.devze.com 2022-12-31 21:42 出处:网络
Maybe the title was not quite exact since I am not sure about how to describe it. I have a user control named NSLTextBlock, below is the definition of NSLTextBlock:

Maybe the title was not quite exact since I am not sure about how to describe it.

I have a user control named NSLTextBlock, below is the definition of NSLTextBlock:

    public partial class NSLTextBlock: UserControl   
{
    public override void OnApplyTemplate()

   {
   base.OnApplyTemplate();
  }
  public object HighlightMatchCase
  {
   get { return GetValue(HighlightMatchCaseProperty); }
   set
   {
    SetValue(HighlightMatchCaseProperty, value);
   }
  }
  public static readonly DependencyProperty HighlightMatchCaseProperty =
   DependencyProperty.Register(
  "HighlightMatchCase",
  typeof(object),
  typeof(NSLTextBlock),
     new PropertyMetadata(null, new PropertyChangedCallback(HighlightChangedCallback)));

  public NSLTextBlock()
  {
   InitializeComponent();
  }
}
}

And the HighlightMatchCase is the Dependency Property.

The host view like this:

<ListView Name ="LogDataList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding LogDataCollection}" Background="Cyan">
        <ListView.View>
            <GridView AllowsColumnReorder="true"
              ColumnHeaderToolTip="Event Log Information">
                <GridViewColumn Header="Event Log Name" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <l:NSLTextBlock Height="25" DataContext="{Binding LogName, Converter={StaticResource DataFieldConverter}}" HighlightMatchCase="{Binding Element}" Loaded="EditBox_Loaded"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Creator" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <l:NSLTextBlock Height="25" DataContext="{Binding CreatorName, Converter={StaticResource DataFieldConverter}}" HighlightMatchCase="{Binding Element}" Loaded="EditBox_Loaded"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Message" Width="100" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <l:NSLTextBlock Height="25" DataContext="{Binding Message, Converter={StaticResource DataFieldConverter}}"  HighlightMatchCase="{Binding Element}" Loaded="EditBox_Loaded"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

DataField:

 public class DataField : INotifyPropertyChanged
    { 
    private HighlightElement  element;
    public HighlightElement Element
    {
      get
      { return element; }
      set
      {
         element = value;
         OnPropertyChanged("Element");
       }
      }
    }

Actually, I want to convert the string(such as LogName, CreatorName and Message) to DataField, and binding my self-defined property(HighlightMatchCase) to Element(the converted datacontext). When I bind it to Element, it throw BindingExpressionException, cannot find property 'Element' on the object.

How can I solve it? Wha开发者_开发问答t is your suggestion?

I really appreciate. On line'in.


The data context of a NSLTextBlock is not set to an instance of DataField for some reason. Add an eventhandler for OnDataContextChanged and with the debugger check what is going on. Maybe it is your DataFieldConverter that fails.

0

精彩评论

暂无评论...
验证码 换一张
取 消