gpt4 book ai didi

c# - 将空值绑定(bind)到 wpf 控件的宽度和高度时出现绑定(bind)错误

转载 作者:行者123 更新时间:2023-11-30 15:03:56 25 4
gpt4 key购买 nike

我们的项目需要绑定(bind)控件的许多属性,例如HeightWidthMinHeightRowColumnrowspan 等。在这样做的同时,我们观察到这些值是 null 时的绑定(bind)错误,我们将从数据库。

为了说明,我的 MainWindow.xaml.cs 看起来像这样。

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//TextWidth id null
TextBlockSize1 = new ItemSize() { TextHeight=20 };
//TextWidth is null
TextBlockSize2 = new ItemSize() { TextWidth = 40 };
//TextHeight is null and TextWidth is null
TextBlockSize3 = new ItemSize() { TextWidth = 40 };
textblock1.DataContext = TextBlockSize1;
textblock2.DataContext = TextBlockSize2;
textblock3.DataContext = TextBlockSize3;
}
public ItemSize TextBlockSize1 { get; set; }

public ItemSize TextBlockSize2 { get; set; }

public ItemSize TextBlockSize3 { get; set; }
}

public class ItemSize
{
public double? TextHeight { get; set; }
public double? TextWidth { get; set; }
}

MainWindow.xaml 看起来像这样。

<Window x:Class="WPfAppln1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:WPfAppln1.Controls"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<TextBlock Name="textblock1" Text=" TextBlock 1" Width="{Binding TextWidth}" Height="{Binding TextHeight}"></TextBlock>
<TextBlock Name="textblock2" Text=" TextBlock 2" Width="{Binding TextWidth}" Height="{Binding TextHeight}"></TextBlock>
<TextBlock Name="textblock3" Text=" TextBlock 3" Width="{Binding TextWidth, TargetNullValue=Auto}" Height="{Binding TextHeight, TargetNullValue=Auto}"></TextBlock>
</StackPanel>
</Window>

在输出窗口中为此显示以下绑定(bind)错误:

System.Windows.Data Error: 5 : 'WPfAppln1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=TextWidth; DataItem='ItemSize' (HashCode=43929715); target element is 'TextBlock' (Name='textblock1'); target property is 'Width' (type 'Double')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=TextHeight; DataItem='ItemSize' (HashCode=57104612); target element is 'TextBlock' (Name='textblock2'); target property is 'Height' (type 'Double')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='<null>' BindingExpression:Path=TextHeight; DataItem='ItemSize' (HashCode=59587750); target element is 'TextBlock' (Name='textblock3'); target property is 'Height' (type 'Double')
The thread '<No Name>' (0x2c60) has exited with code 0 (0x0).

由于这些错误,应用程序需要很长时间才能加载屏幕。

因此,问题是如何在绑定(bind)到 wpf 控件时容纳可为 null 的值,以及当绑定(bind)值为 null 时如何为控件的宽度属性提供默认值,例如“auto”。

最佳答案

您可以使用 TargetNullValue (如果源为空)或 FallbackValue (以防绑定(bind)失败,例如 DataContextnull)

更新:

感谢 Dean 指出这一点,我错误地认为 Auto 会起作用(即 TypeConverter 会负责转换)。

但是,您仍然可以使用自动属性并使用 x:Static Markup Extension 在 XAML 中提供 Auto 值像这样 -

<TextBlock Name="textblock1" Text=" TextBlock 1"  
Height="{Binding TextHeight, TargetNullValue={x:Static System:Double.NaN},
FallbackValue={x:Static System:Double.NaN}}">
</TextBlock>

Value="{x:Static System:Double.NaN}" 也可以像这样用在 DataTrigger 方法中 -

<TextBlock.Style>
<Style>
<Setter Property="Control.Width" Value="{Binding Path=TextWidth}" />
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=TextWidth}"
Value="{x:Null}">
<Setter Property="Control.Width" Value="{x:Static System:Double.NaN}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>

注意:将需要这个命名空间-

xmlns:System="clr-namespace:System;assembly=mscorlib" 

旧代码:

<TextBlock Name="textblock1" Text=" TextBlock 1"  Width="{Binding TextWidth}"  
Height="{Binding TextHeight, TargetNullValue=Auto, FallbackValue=Auto }">
</TextBlock>

另一种解决方案是使用这样的触发器 -

<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=TextWidth}"
Value="{x:Null}">
<Setter Property="Control.Width" Value="Auto" />
</DataTrigger>
<DataTrigger
Binding="{Binding Path=TextHeight}"
Value="{x:Null}">
<Setter Property="Control.Height" Value="Auto" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>

关于c# - 将空值绑定(bind)到 wpf 控件的宽度和高度时出现绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10996189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com