@@ -305,7 +305,7 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
305
305
306
306
{% highlight XAML %}
307
307
308
- <Window x: Class ="TemplateSelector_ButtonAdv.MainWindow"
308
+ <Window x:Class="TemplateSelector_ButtonAdv.MainWindow"
309
309
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
310
310
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
311
311
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
314
314
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
315
315
mc:Ignorable="d"
316
316
Title="MainWindow" Height="450" Width="800">
317
+
318
+ <Window.DataContext>
319
+ <local:ViewModel/>
320
+ </Window.DataContext>
321
+
317
322
<Window.Resources>
318
- <DataTemplate x:Key =" newIcon " >
323
+ <DataTemplate x:Key="checkedIcon ">
319
324
<Grid Width="12" Height="16">
320
325
<Path
321
326
Margin="0.5"
@@ -328,7 +333,7 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
328
333
Stretch="Fill" />
329
334
</Grid>
330
335
</DataTemplate>
331
- <DataTemplate x:Key =" OpenIcon " >
336
+ <DataTemplate x:Key="unCheckedIcon ">
332
337
<Grid Width="16" Height="16">
333
338
<Path
334
339
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
341
346
Stretch="Fill" />
342
347
</Grid>
343
348
</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 }"/>
345
350
</Window.Resources>
351
+
346
352
<Grid>
347
353
<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 }"/>
350
356
</StackPanel>
351
357
</Grid>
352
358
</Window >
@@ -355,28 +361,49 @@ The [IconTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.C
355
361
356
362
{% highlight c# %}
357
363
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
363
365
{
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
+ }
373
399
}
374
- }
375
400
376
401
{% endhighlight %}
377
402
378
403
{% endtabs %}
379
404
405
+ ![ IconTemplateSelector in WPF Button] ( Getting-Started_images/Getting-Started_IconTemplateSelector.gif )
406
+
380
407
N> The [ ButtonAdv] ( https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.ButtonAdv.html ) loads the icon in the following priority order.
381
408
* [ IconTemplateSelector] ( https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.ButtonAdv.html#Syncfusion_Windows_Tools_Controls_ButtonAdv_IconTemplateSelector )
382
409
* [ IconTemplate] ( https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.ButtonAdv.html#Syncfusion_Windows_Tools_Controls_ButtonAdv_IconTemplate )
0 commit comments