[WPF] StaticResource not applied inside DataTemplate but works outside
Hi!
I’m using WPF UI and I’m trying to set the NumberFormatter property on the NumberBox control. For some reason, when the NumberBox is created inside a DataTemplate, the NumberFormatter I set is not applied (it looks like it keeps the default formatter).
In the example below, both NumberBox controls reference the same NumberFormatter defined in Page.Resources, but only the second one (outside the DataTemplate) actually uses it.
This feels like a WPF templating/resource-resolution quirk rather than a WPF UI issue, but I’m not sure what I’m missing. Any help would be appreciated!
I’m using WPF UI and I’m trying to set the NumberFormatter property on the NumberBox control. For some reason, when the NumberBox is created inside a DataTemplate, the NumberFormatter I set is not applied (it looks like it keeps the default formatter).
In the example below, both NumberBox controls reference the same NumberFormatter defined in Page.Resources, but only the second one (outside the DataTemplate) actually uses it.
This feels like a WPF templating/resource-resolution quirk rather than a WPF UI issue, but I’m not sure what I’m missing. Any help would be appreciated!
xaml
<Page
x:Class="Wpf.Ui.Gallery.Views.Pages.Text.NumberBoxPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Wpf.Ui.Gallery.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="clr-namespace:Wpf.Ui.Gallery.Helpers"
xmlns:local="clr-namespace:Wpf.Ui.Gallery.Views.Pages.Text"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="NumberBoxPage"
controls:PageControlDocumentation.DocumentationType="{x:Type ui:NumberBox}"
d:DataContext="{d:DesignInstance local:NumberBoxPage,
IsDesignTimeCreatable=False}"
d:DesignHeight="450"
d:DesignWidth="800"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d">
<Page.Resources>
<helpers:NumberFormatter x:Key="NumberFormatter" />
<DataTemplate x:Key="TestDataTemplate">
<controls:ControlExample
Margin="0"
HeaderText="WPF UI NumberBox."
XamlCode="<ui:NumberBox PlaceholderText="Enter your age" />">
<ui:NumberBox NumberFormatter="{StaticResource NumberFormatter}" PlaceholderText="Enter your age" />
</controls:ControlExample>
</DataTemplate>
</Page.Resources>
<StackPanel Margin="0,0,0,24">
<ContentControl ContentTemplate="{StaticResource TestDataTemplate}" />
<controls:ControlExample
Margin="0"
HeaderText="WPF UI NumberBox."
XamlCode="<ui:NumberBox PlaceholderText="Enter your age" />">
<ui:NumberBox NumberFormatter="{StaticResource NumberFormatter}" PlaceholderText="Enter your age" />
</controls:ControlExample>
</StackPanel>
</Page>xaml
<Page
x:Class="Wpf.Ui.Gallery.Views.Pages.Text.NumberBoxPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Wpf.Ui.Gallery.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="clr-namespace:Wpf.Ui.Gallery.Helpers"
xmlns:local="clr-namespace:Wpf.Ui.Gallery.Views.Pages.Text"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="NumberBoxPage"
controls:PageControlDocumentation.DocumentationType="{x:Type ui:NumberBox}"
d:DataContext="{d:DesignInstance local:NumberBoxPage,
IsDesignTimeCreatable=False}"
d:DesignHeight="450"
d:DesignWidth="800"
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}"
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
mc:Ignorable="d">
<Page.Resources>
<helpers:NumberFormatter x:Key="NumberFormatter" />
<DataTemplate x:Key="TestDataTemplate">
<controls:ControlExample
Margin="0"
HeaderText="WPF UI NumberBox."
XamlCode="<ui:NumberBox PlaceholderText="Enter your age" />">
<ui:NumberBox NumberFormatter="{StaticResource NumberFormatter}" PlaceholderText="Enter your age" />
</controls:ControlExample>
</DataTemplate>
</Page.Resources>
<StackPanel Margin="0,0,0,24">
<ContentControl ContentTemplate="{StaticResource TestDataTemplate}" />
<controls:ControlExample
Margin="0"
HeaderText="WPF UI NumberBox."
XamlCode="<ui:NumberBox PlaceholderText="Enter your age" />">
<ui:NumberBox NumberFormatter="{StaticResource NumberFormatter}" PlaceholderText="Enter your age" />
</controls:ControlExample>
</StackPanel>
</Page>