Closed mzy666888 closed 4 years ago
@mzy666888 Here is my demo and I find not only the Lable but also the Button has the same behavior.
<StackPanel>
<Button Width="100" Height="100" Content="{Binding Three, StringFormat={}{0:F2}}" />
<Label HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="36" Content="{Binding Three, StringFormat={}{0:F1}}" />
<Label HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="36">
<Label.Content>
<TextBlock Text="{Binding Three, StringFormat={}{0:F1}}" />
</Label.Content>
</Label>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="36"
Text="{Binding Four, StringFormat={}{0:F1}}" />
</StackPanel>
public MainWindow()
{
InitializeComponent();
Three = 3.0;
Four = 3.0;
DataContext = this;
}
public static readonly DependencyProperty FourProperty = DependencyProperty.Register(
"Four", typeof(double), typeof(MainWindow), new PropertyMetadata(default(double)));
public double Four
{
get { return (double) GetValue(FourProperty); }
set { SetValue(FourProperty, value); }
}
public static readonly DependencyProperty ThreeProperty = DependencyProperty.Register(
"Three", typeof(double), typeof(MainWindow), new PropertyMetadata(default(double)));
public double Three
{
get { return (double) GetValue(ThreeProperty); }
set { SetValue(ThreeProperty, value); }
}
And I add the Trace to Label Content and find the code.
System.Windows.Data Warning: 56 : Created BindingExpression (hash=28278595) for Binding (hash=63993496)
System.Windows.Data Warning: 58 : Path: 'Three'
System.Windows.Data Warning: 60 : BindingExpression (hash=28278595): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=28278595): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=28278595): Attach to System.Windows.Controls.Label.Content (hash=36936550)
System.Windows.Data Warning: 67 : BindingExpression (hash=28278595): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=28278595): Found data context element: Label (hash=36936550) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=28278595): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=28278595): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=28278595): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=28278595): Found data context element: Label (hash=36936550) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=28278595): Activate with root item MainWindow (hash=62468121)
System.Windows.Data Warning: 107 : BindingExpression (hash=28278595): At level 0 using cached accessor for MainWindow.Three: DependencyProperty(Three)
System.Windows.Data Warning: 104 : BindingExpression (hash=28278595): Replace item at level 0 with MainWindow (hash=62468121), using accessor DependencyProperty(Three)
System.Windows.Data Warning: 101 : BindingExpression (hash=28278595): GetValue at level 0 from MainWindow (hash=62468121) using DependencyProperty(Three): '3'
System.Windows.Data Warning: 80 : BindingExpression (hash=28278595): TransferValue - got raw value '3'
System.Windows.Data Warning: 89 : BindingExpression (hash=28278595): TransferValue - using final value '3'
And then I add the trace to TextBlock and find the code
System.Windows.Data Warning: 56 : Created BindingExpression (hash=11958757) for Binding (hash=12674872)
System.Windows.Data Warning: 58 : Path: 'Three'
System.Windows.Data Warning: 60 : BindingExpression (hash=11958757): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=11958757): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=11958757): Attach to System.Windows.Controls.TextBlock.Text (hash=8864859)
System.Windows.Data Warning: 67 : BindingExpression (hash=11958757): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=11958757): Found data context element: TextBlock (hash=8864859) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=11958757): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=11958757): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=11958757): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=11958757): Found data context element: TextBlock (hash=8864859) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=11958757): Activate with root item MainWindow (hash=62468121)
System.Windows.Data Warning: 107 : BindingExpression (hash=11958757): At level 0 using cached accessor for MainWindow.Three: DependencyProperty(Three)
System.Windows.Data Warning: 104 : BindingExpression (hash=11958757): Replace item at level 0 with MainWindow (hash=62468121), using accessor DependencyProperty(Three)
System.Windows.Data Warning: 101 : BindingExpression (hash=11958757): GetValue at level 0 from MainWindow (hash=62468121) using DependencyProperty(Three): '23'
System.Windows.Data Warning: 80 : BindingExpression (hash=11958757): TransferValue - got raw value '23'
System.Windows.Data Warning: 84 : BindingExpression (hash=11958757): TransferValue - implicit converter produced '23.0'
System.Windows.Data Warning: 89 : BindingExpression (hash=11958757): TransferValue - using final value '23.0'
I find the TextBlock will use System.Windows.Data Warning: 84 : BindingExpression (hash=11958757): TransferValue - implicit converter produced '23.0'
to convert '23' to '23.0' and I think it is why the Label Content show without the format code.
And then I run with .NET Framework 4.7 and find the same behavior
Duplicate of #681 (This is by design, as Content
is of type object
and does not need a converter.)
Security issues and bugs should be reported privately, learn more via our responsible disclosure guidelines.
Problem description: in .net core wpf ,I use Label and TextBlock like:
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" Foreground="White" Text="{Binding Four, StringFormat={}{0:F1}}">