Question

I have roblem with timer and AutocompleteBox toolkit. I have AutoCompletebox with TextChanged Event. On this event I start timer to make filtering smoother (when user writes, filter don't work). When I writes everything works ok, but when I choose from DropDown list TextChanged event starts, but I don't have timer Tick event (filter don't starts). What I do wrong?

xaml:

<Controls:AutoCompleteBox Name="acbIdentyfikatorPcS" ValueMemberPath="Identyfikator" FilterMode="Contains" HorizontalAlignment="Left" Margin="100,5,0,0" Grid.Row="1" VerticalAlignment="Top" Width="121" ToolTip="Identyfikator" MinimumPrefixLength="0" TextChanged="acbSerwisant_TextChanged" IsTextCompletionEnabled="True">
                    <Controls:AutoCompleteBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Margin="0,0,10,0" FontWeight="Bold" Text="{Binding Identyfikator}"></TextBlock>
                        </DataTemplate>
                    </Controls:AutoCompleteBox.ItemTemplate>
                </Controls:AutoCompleteBox>

CS: Declaration:

private DispatcherTimer timerfiltr = new DispatcherTimer();
private DataTable PcS;  

public MainWindow()
{
    InitializeComponent();
    timerfiltr.Tick += new EventHandler(timerfiltr_Tick);
    timerfiltr.Interval = new TimeSpan(0, 0, 0, 0, 400);
} 


private void acbSerwisant_TextChanged(object sender, RoutedEventArgs e)
{
    timerfiltr.Stop();
    timerfiltr.IsEnabled = true;
    timerfiltr.Start();     
}

private void timerfiltr_Tick(object sender, EventArgs e)
{
    PcS.DefaultView.RowFilter = "Identyfikator like '%" + acbIdentyfikatorPcS.Text + "%'";
    timerfiltr.Stop();
    timerfiltr.IsEnabled = false;            
}

EDIT:

I think I found my problem: This AutoCompleteBox is on one of the TabItem, I have also TabControl SelectionChanged event, where is also timer.stop() command

 private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
    //rest of code
           // timerfiltr.Stop();
    }

When I press key down on keyboard (AutoCompleteBox is focused), DropDown opens, and then also TabControl SelectionChanged event also starts and stops timer... Weird

No correct solution

OTHER TIPS

You can use the IsDropDownOpen property (or DropDownXXX events) of the AutoCompleteBox to figure out whether the DropDown is currently visible and one quick solution is to avoid stopping your timer inside its Tick method for example.

I didn't test your sample code, I think maybe *acbSerwisant_TextChanged* stop the dispatcher thread. For this scenario, I suggest you do change your code like this, maybe not best, but should work, and there is framework is more suitable for dealing this which is called "RX"(https://rx.codeplex.com), method Throttle is what your want.

**Update: if you worry about perf, add flag as switch,since all code run under ui thread, this guarantee you that last text change will be accepted.But reality is a bit complex, normally you will do you filter in another thread...if that happened,i suggest you use other thread with while statement to check textchange, or you can take look at RX.

private bool enableFilter;
public MainWindow()
{
  InitializeComponent();
  timerfiltr.Tick += new EventHandler(timerfiltr_Tick);
  timerfiltr.Interval = new TimeSpan(0, 0, 0, 0, 400);

  acbIdentyfikatorPcS.GotFocus +=(s,e)=>{timerfiltr.Start();};
  acbIdentyfikatorPcS.LostFocus +=(s,e)=>{timerfiltr.Stop();};

  acbIdentyfikatorPcS.TextChanged +=(s,e)=>{enableFilter= true;};

} 

private void timerfiltr_Tick(object sender, EventArgs e)
{
    if(enableFilter)
    {
        enableFilter= false;           
        //do filter

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top