我正在為這個錯誤 N°5 苦苦掙扎。這是完整的錯誤訊息:
System.Windows.Data 錯誤:5:BindingExpression 生成的值對于目標屬性無效。值='0' BindingExpression:Path=ActualHeight; DataItem='Grid' (Name='gridEingamaske'); 目標元素是 'TextBox' (Name='txtabmessungStecknuss'); 目標屬性是“FontSize”(型別“Double”)
這是我到目前為止所想出的:我有一個 WPF-Control,它承載了很多文本框(具體來說是 118 個)。我還使用 FontSizeConverter 來操作文本框中的 FontSize。這個 FontSizeConverter 似乎導致了問題,因為它期望一個雙值但得到一個 0(整數)。
我怎樣才能解決這個問題?
我的 xaml 代碼(最后只給你 118 個文本框中的 1 個):
<UserControl x:Class="SchrauberDB.Controls.Eingabemaske"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SchrauberDB.View"
xmlns:dm="clr-namespace:SchrauberDB.DataModel"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="1100"
FontFamily="Arial">
<UserControl.Resources>
<dm:FontSizeConverter x:Key="fontSizeCon" />
<Style TargetType="{x:Type Grid}">
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="50"/>
</Style>
</Style.Resources>
</Style>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="20,0,0 ,0" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="18" />
<Setter Property="FontFamily" Value="Arial" />
<Setter Property="FontWeight" Value="Medium" />
</Style>
</Style.Resources>
</Style>
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="BorderBrush" Value="#6A767D" />
</Style>
</Style.Resources>
<Setter Property="Width" Value="160" />
<Setter Property="Height" Value="30" />
<Setter Property="BorderBrush" Value="#6A767D" />
<Setter Property="Background" Value="#C2CACF" />
</Style>
</UserControl.Resources>
<Grid x:Name="gridEingabemaske" Background="#FEFFFF" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"/>
<ColumnDefinition />
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Column="3" Grid.Row="0"
Height="45" Width="160">
<TextBlock FontSize="14" FontWeight="Medium">
Speichern
</TextBlock>
</Button>
<Button Grid.Column="3" Grid.Row="1"
Height="45" Width="160" Click="Button_CreateBemi_Click"
>
<TextBlock FontSize="14" FontWeight="Medium">
Neues Bemi Anlegen
</TextBlock>
</Button>
<ListView x:Name="lvEingabe" Margin="25" Grid.RowSpan="2">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
<StackPanel x:Name="abmessungStecknuss" Visibility="Hidden">
<TextBlock>Abmessung Stecknuss</TextBlock>
<TextBox x:Name="txtabmessungStecknuss" Text="{Binding Path=abmessungStecknuss}" KeyDown="OnKeyDownHandler" FontSize="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Converter={StaticResource fontSizeCon}}"></TextBox>
</StackPanel>
...
這是我的 FontSizeConverter 類:
namespace SchrauberDB.DataModel
{
public class FontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double actualHeight = System.Convert.ToDouble(value);
int fontSize = (int)(actualHeight * .05);
return fontSize;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
uj5u.com熱心網友回復:
從doubleto的演員表int是多余的。你應該洗掉它。此外,不要拋出NotImplementedException例外。如果ConvertBack不支持該方法,則拋出NotSupportedException.
這里的問題是,根據錯誤訊息,ActualHeight 的值為0。雖然0是一個有效的高度,但它是 的非法值FontSize。
訊息:
“由 BindingExpression 產生的值對目標無效”
然后它宣告該值為
“Value='0'”
目標是
“目標元素是 'TextBox'”
目標屬性
“目標屬性是 'FontSize'”
該訊息明確傳達了無效值而不是無效型別!
錯誤訊息轉換為:“該值0對該TextBox.FontSize屬性無效。”
掌握了這些知識后,您現在可以查閱該Control.FontSize屬性的 API 參考。從這里你可以學到:
字體大小必須是正數。
從我們的數學課上我們知道零不是正數。
發現問題。解決方案很明顯:防止非法轉換器值。
這意味著,您應該為轉換器實作添加一些健壯性:
public class FontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double actualHeight = System.Convert.ToDouble(value);
double fontSize = actualHeight * .05;
return Math.Max(0.1, fontSize);
// Alternatively return a default value e.g., 12
return fontsize == 0 ? 12 : fontsize
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/399700.html
