@@ -305,7 +305,7 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
305305
306306 {% highlight XAML %}
307307
308- <Window x: Class ="TemplateSelector_ButtonAdv.MainWindow"
308+ <Window x:Class="TemplateSelector_ButtonAdv.MainWindow"
309309 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
310310 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
311311 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -314,8 +314,13 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
314314 xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
315315 mc:Ignorable="d"
316316 Title="MainWindow" Height="450" Width="800">
317+
318+ <Window.DataContext>
319+ <local:ViewModel/>
320+ </Window.DataContext>
321+
317322 <Window.Resources>
318- <DataTemplate x:Key =" newIcon " >
323+ <DataTemplate x:Key="checkedIcon ">
319324 <Grid Width="12" Height="16">
320325 <Path
321326 Margin="0.5"
@@ -328,7 +333,7 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
328333 Stretch="Fill" />
329334 </Grid>
330335 </DataTemplate>
331- <DataTemplate x:Key =" OpenIcon " >
336+ <DataTemplate x:Key="unCheckedIcon ">
332337 <Grid Width="16" Height="16">
333338 <Path
334339 Margin="0.5,0.5,0.738,0.502"
@@ -341,12 +346,13 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
341346 Stretch="Fill" />
342347 </Grid>
343348 </DataTemplate>
344- <local: TemplateSelector x: Key ="IconTemp" NewIcon ="{StaticResource newIcon }" OpenIcon ="{StaticResource OpenIcon }"/>
349+ <local:IconTemplateSelector x:Key="IconTemplateSelector" CheckedIcon ="{StaticResource checkedIcon }" UnCheckedIcon ="{StaticResource unCheckedIcon }"/>
345350 </Window.Resources>
351+
346352 <Grid>
347353 <StackPanel VerticalAlignment="Center">
348- <CheckBox Name =" Check " IsChecked =" True " Checked = " Check_Checked " Unchecked = " Check_Unchecked " HorizontalAlignment =" Center " Command = " {Binding CheckCommand} " Content =" ChangeIcon " />
349- <syncfusion: ButtonAdv HorizontalAlignment="Center" Margin="10" Content="{Binding IsChecked}" Label=" IconTemplateSelector" IconTemplateSelector ="{StaticResource IconTemp }"/>
354+ <CheckBox Name="Check" IsChecked="{Binding IsChecked, Mode=TwoWay}" HorizontalAlignment="Center" Content="ChangeIcon"/>
355+ <syncfusion:ButtonAdv x:Name="button" HorizontalAlignment="Center" Margin="10" Label="IconTemplateSelector" IconTemplateSelector="{StaticResource IconTemplateSelector}" DataContext ="{Binding IsChecked }"/>
350356 </StackPanel>
351357 </Grid>
352358 </Window >
@@ -355,28 +361,49 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
355361
356362 {% highlight c# %}
357363
358- public class TemplateSelector : DataTemplateSelector
359- {
360- public DataTemplate NewIcon { get; set; }
361- public DataTemplate OpenIcon { get; set; }
362- public override DataTemplate SelectTemplate(object item, DependencyObject container)
364+ public class ViewModel : INotifyPropertyChanged
363365 {
364- if (item == null)
365- {
366- return OpenIcon;
367- }
368- if ((item as Model).IsChecked)
369- {
370- return NewIcon;
371- }
372- return base.SelectTemplate(item, container);
366+ private bool _isChecked;
367+ public bool IsChecked
368+ {
369+ get { return _isChecked; }
370+ set
371+ {
372+ if (_isChecked != value)
373+ {
374+ _isChecked = value;
375+ OnPropertyChanged(nameof(IsChecked));
376+ }
377+ }
378+ }
379+
380+ public event PropertyChangedEventHandler PropertyChanged;
381+
382+ protected void OnPropertyChanged(string propertyName) =>
383+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
384+ }
385+
386+ public class IconTemplateSelector : DataTemplateSelector
387+ {
388+ public DataTemplate CheckedIcon { get; set; }
389+ public DataTemplate UnCheckedIcon { get; set; }
390+
391+ public override DataTemplate SelectTemplate(object item, DependencyObject container)
392+ {
393+ if (item is bool isChecked)
394+ {
395+ return isChecked ? CheckedIcon : UnCheckedIcon;
396+ }
397+ return base.SelectTemplate(item, container);
398+ }
373399 }
374- }
375400
376401 {% endhighlight %}
377402
378403 {% endtabs %}
379404
405+ ![ IconTemplateSelector in WPF Button] ( Getting-Started_images/Getting-Started_IconTemplateSelector.gif )
406+
380407 N> The [ ButtonAdv] ( https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.ButtonAdv.html ) loads the icon in the following priority order.
381408* [ IconTemplateSelector] ( https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.ButtonAdv.html#Syncfusion_Windows_Tools_Controls_ButtonAdv_IconTemplateSelector )
382409* [ IconTemplate] ( https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.ButtonAdv.html#Syncfusion_Windows_Tools_Controls_ButtonAdv_IconTemplate )
0 commit comments