Well you can write a workaround, but I strongly recomment not to implement it that way. Consider, that a style in a ResourceDictionary
is an empty resource, which should be decoupled from any specific instance (in your case myListBox
) in your application. Problem is, that you cannot use this malformed style on another Button
. So you don't need to, better you shouldn't, declare it as resource.
I definitely recomment to declare this Style
directly in the Button
. E.g.
<ListBox x:Name="myListBox" />
<Button>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myListBox,
Path=SelectedItems.Count}" Value="25">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Additionally, I would use a Binding
via the ElementName
property.