我有一個名為LoadAnimAngle的 double ,它簡單地保存旋轉加載圖示的角度,隨著時間的推移旋轉。這個變數在我的MainViewModel類中定義。我在所有具有旋轉加載圖示的地方都使用了相同的變數。
我需要它在Generic.xml中使用樣式/模板定義的自定義控制元件中。這是我系結到LoadAnimAngle的部分
<v:ColoredImage Image="{StaticResource LoadingIcon}" Color="{StaticResource DarkBlueClick}" RenderTransformOrigin="0.5, 0.5" VerticalAlignment="Center" Width="32" Height="32" Margin="0,0,0,0" Visibility="{Binding IsBusy, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibility}}">
<v:ColoredImage.RenderTransform>
<RotateTransform Angle="{Binding MainViewModel.LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}}"/> //here is the error
</v:ColoredImage.RenderTransform>
</v:ColoredImage>
自定義控制元件具有系結到我的 MainViewModel 實體的屬性,如下所示:
public MainViewModel MainViewModel { get { return MainViewModel.instance; } }
在 MainViewModel 的建構式中,我簡單地設定:
instance = this;
問題是Generic.xml在我的MainViewModel類之前加載,導致在加載圖形之前幀的實體為空,加載完所有內容后,一切正常。我該如何解決這個問題?
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MainViewModel.LoadAnimAngle; DataItem=null; target element is 'RotateTransform' (HashCode=66939890); target property is 'Angle' (type 'Double')
請注意,我確實理解該錯誤是無害的,并且不會對最終用戶產生任何影響,但是每次除錯時看到該錯誤都會讓我感到痛苦
我需要在 Generic 之前以某種方式加載 MainViewModel,或者告訴 xaml 在 MainViewModel != null 之前不要嘗試從 LoadAnimAngle 獲取資料。
編輯
進行更改后,我得到了同樣的錯誤,因此我沒有直接系結到 MainViewModel 的實體。所以我認為我對問題案例的評價是錯誤的。
我添加了
public double LoadAnimAngle
{
get
{
if (MainViewModel.instance != null)
{
return MainViewModel.instance.LoadAnimAngle;
}
return 0;
}
}
到 ViewModel(而不是回傳 MainViewModel.instance )
然后我將系結更改為:
Angle="{Binding Path=LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}"
我犯了同樣的錯誤:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=LoadAnimAngle; DataItem=null; target element is 'RotateTransform' (HashCode=21529561); target property is 'Angle' (type 'Double')
如果問題不是 MainViewModel.instance 為 NULL,那么導致問題的原因是什么?我在解碼錯誤訊息中的語言時遇到問題。究竟出了什么問題,為什么?
編輯 2 相關背景關系(?)
<Style TargetType = "{x:Type v:ComPortButton}" >
<Setter Property = "Background" Value = "{StaticResource Milky}"/>
<Setter Property = "ColorPalette" Value = "{StaticResource MilkyPalette}"/>
<Setter Property = "Foreground" Value = "{StaticResource Black}"/>
<Setter Property = "BorderColor" Value = "{StaticResource Milky}"/>
<Setter Property="IsBasicTextButton" Value="False"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type v:ComPortButton}">
<Grid>
<Grid Visibility="{Binding Path=IsBasicTextButton, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBooleanToVisibility}}">
<Border BorderBrush="{TemplateBinding BorderColor}" Background="{TemplateBinding Background}" Width="128" Height="140" BorderThickness="1"/>
//REMOVED IREELEVANT CODE
<v:ColoredImage Image="{StaticResource LoadingIcon}" Color="{StaticResource DarkBlueClick}" RenderTransformOrigin="0.5, 0.5" VerticalAlignment="Center" Width="32" Height="32" Margin="0,0,0,0" Visibility="{Binding IsBusy, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibility}}">
<v:ColoredImage.RenderTransform>
<RotateTransform Angle="{Binding MainViewModel.LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}}"/> //here is the error
</v:ColoredImage.RenderTransform>
</v:ColoredImage>
</Grid>
//REMOVED IRRELEVANT CONTROL
</Grid>
//REMOVED IRRELEVANT CONTEXT MENU
</Grid>
//REMOVED IRRELEVANT TRIGGERS
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
編輯 3 錯誤的來源似乎與我最初的想法完全不同。該錯誤似乎與 RenderTransform 有關,因為我可以從其他地方訪問該屬性而不會出現錯誤。像這樣:
//NO ERROR FOR TEXT BLOCK
<TextBlock Text="{Binding MainViewModel.LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}"/>
<v:ColoredImage Image="{StaticResource LoadingIcon}" Color="{StaticResource DarkBlueClick}" RenderTransformOrigin="0.5, 0.5" VerticalAlignment="Center" Width="32" Height="32" Margin="0,0,0,0" Visibility="{Binding IsBusy, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibility}}">
<v:ColoredImage.RenderTransform>
//ERROR FOR ROTATETRANSFORM
<RotateTransform Angle="{Binding MainViewModel.LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}"/>
</v:ColoredImage.RenderTransform>
</v:ColoredImage>
但是當我不參考 MainViewModel 時,我也會收到錯誤訊息。我創建了一個這樣的新屬性:
public double LoadAnimAngle
{
get
{
return 0;
}
}
然后我像這樣在模板中使用它:
<v:ColoredImage Image="{StaticResource LoadingIcon}" Color="{StaticResource DarkBlueClick}" RenderTransformOrigin="0.5, 0.5" VerticalAlignment="Center" Width="32" Height="32" Margin="0,0,0,0" Visibility="{Binding IsBusy, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibility}}">
<v:ColoredImage.RenderTransform>
<RotateTransform Angle="{Binding LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}"/>
</v:ColoredImage.RenderTransform>
</v:ColoredImage>
但我得到了完全相同的錯誤!
因此,財產有效,一切正常。只是RenderTransform在實體化時第一幀就像在VisualTree之外?或者類似的東西,我猜?RenderTransform 中發生了一些不同的事情,使它不喜歡我的系結。
而且我可能不清楚結構。
ComPortButton是一個自定義控制元件(Generic.xml 中帶有模板/樣式的.cs 檔案)。 ComPortButton使用ComPortVM作為它的DataContext。我想全域訪問旋轉值,不同的控制元件,不同的視窗,不同的一切,全域。我有一個MainViewModel我目前在其中存盤值,因為它提供全域訪問,因為它
編輯 4 解決了它并在下面發布了解決方案
uj5u.com熱心網友回復:
在我發現問題是 RenderTransform 而不是其他問題之后,很容易在網上找到解決方案,似乎很多人都遇到了同樣的問題。
這是幫助我解決它的執行緒
問題與 VisualTree 有關,模板中的 RenderTransform 在加載整個控制元件之前沒有連接到 VisualTree。或類似的東西。
當像這樣系結到 RotateTransform 時:
<v:ColoredImage.RenderTransform>
<RotateTransform Angle="{Binding LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}}"/>
</v:ColoredImage.RenderTransform>
出現問題。但是由于某些我不明白的原因,您可以通過系結到 RenderTransform 來消除錯誤。但為此,您需要一個轉換器。
[ValueConversion(typeof(double), typeof(RotateTransform))]
public class AngleToTransform : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return new RotateTransform((double)value);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
然后像這樣使用轉換器:
<v:ColoredImage RenderTransform="{Binding MainViewModel.LoadAnimAngle, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource AngleToTransform}}"/>
uj5u.com熱心網友回復:
您的控制元件必須獨立于具體的視圖模型型別。相反,將控制元件的內部系結到此控制元件上的依賴項屬性。然后讓外部視圖模型系結到這個屬性(或在本地設定它們)。
通過這種方式,您可以消除控制元件和 之間的緊密耦合DataContext,從而大大簡化了控制元件的實作。它還允許控制元件與任何DataContext(視圖模型)重用。
ComPortButton.cs
class ComPortButton : Control
{
public double Angle
{
get => (double)GetValue(AngleProperty);
set => SetValue(AnglePropertyKey, value);
}
protected static readonly DependencyProperty AnglePropertyKey = DependencyProperty.RegisterReadOnly(
"Angle",
typeof(double),
typeof(ComPortButton),
new PropertyMetadata(default));
public static readonly DependencyProperty AngleProperty = AnglePropertyKey..DependencyProperty;
public double ProgressPercentage
{
get => (double)GetValue(ProgressPercentageProperty);
set => SetValue(ProgressPercentageProperty, value);
}
public static readonly DependencyProperty ProgressPercentageProperty = DependencyProperty.Register(
"ProgressPercentage",
typeof(double),
typeof(ComPortButton),
new PropertyMetadata(default(double), OnProgressPercentageChanged));
private static void OnProgressPercentageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
double percentage = (double)e.NewValue / 100;
// Enforce an angle between 0°-360°
this.Angle = Math.Max(0, Math.Min(360, 360 * percentage));
}
}
通用的.xaml
<Style TargetType="ComPortButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type v:ComPortButton}">
<v:ColoredImage>
<v:ColoredImage.RenderTransform>
<RotateTransform Angle="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Angle}" />
</v:ColoredImage.RenderTransform>
</v:ColoredImage>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
使用示例
<Window>
<Window.DataContext>
<MainViewModel />
</Window.DataContext>
<ComPortButton ProgressPercentage="{Binding ProgressPercentageValue}" />
</Window>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471397.html
