What you want to do is a trigger.
You would like to use the default background, but override it when a given property meet a given condition. You can do this easily with a trigger.
Simply add a property such as this one to your view model:
public bool OverrideBackground { get { return backgroundString != ""; } }
Then add the following trigger in your DataTemplate
:
<DataTemplate>
[...]
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding OverrideBackground}" Value="true">
<Setter Property="Button.Background" Value="{Binding background}" TargetName="btn"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
The DataTrigger
will be activated when the OverrideBackground
property is true (that is, when backgroundString != ""
), and will set the Background
property of the Button
(that you named btn
in your code snippet) to the value of the background
property of the bound view model.