views:

33

answers:

1

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? What is your suggestion?

I really appreciate. On line'in.

A: 

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.

Wallstreet Programmer
I debugged the datacontext with diagnostics and really worked for me.I have payed much attention to the BindingExpression exception! Actually it works fine! Wallstreet Programmer, JustABill, thank you very much saved my night time.