[EDIT] * avevo dimenticato di includere l'eccezione restituitoCome cambiare WPF Rettangolo Stile Programatically (MVVM) (noob)
'System.Windows.Style' is not a valid value for property 'Fill'
*
questo è il mio primo post, ho già fatto un sacco di ricerca di domande in cui questo potrebbe essere già stato risposto a nessun successo. Sono un vero e proprio noob quando si tratta di MVVM, Prism e WPF e sono stato buttato nel profondo su questo.
Desidero modificare il contenuto di 2 rettangoli a seconda di un valore booleano nel modello (rilegatura passata).
se un test restituisce Pass (bool True), quindi viene visualizzato il colore Verde per il primo rettangolo e il secondo rettangolo visualizza UserControl.Resource Style x: Key = "RectanglePass". Se il test fallisce, allora primo rettangolo è rosso, e il secondo display UserControl.Resource Style x: Key = "RectangleFail"
Ho il seguente codice XAML:
<UserControl x:Class="Integration.Memjet.Aoqc.Views.ProcessResultView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style x:Key="RectangleFail" TargetType="Rectangle">
<Setter Property="Fill">
<Setter.Value>
<VisualBrush>
....
</VisualBrush>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RectanglePass" TargetType="Rectangle">
<Setter Property="Fill">
<Setter.Value>
<VisualBrush>
....
</VisualBrush>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20">PASS</Label>
<Rectangle Name="rectStatus" Grid.Row="1">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Passed}" Value="True">
<Setter Property="Fill" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Passed}" Value="False">
<Setter Property="Fill" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<Rectangle Name="rectStatusImg" Width="120" Height="120" Grid.Row="2" >
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Passed}" Value="True">
<Setter Property="Fill" Value="{StaticResource RectanglePass}" />
</DataTrigger>
<DataTrigger Binding="{Binding Passed}" Value="False">
<Setter Property="Fill" Value="{StaticResource RectangleFail}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Grid>
</UserControl>
È possibile che questo non funziona e restituisce un'eccezione. Se dovessi sostituire il secondo tag Rettangolo (rectStatusImg) con questa linea;
<Rectangle Name="rectStatusImg" Style="{StaticResource RectanglePass}" Width="120" Height="120" Grid.Row="2" ></Rectangle>
xaml funziona e produce il risultato previsto! Ma desidero legarlo a Boolean Passed così posso cambiarlo a livello di programmazione.
Spero davvero di essere stato in grado di spiegare il mio problema qui.
Grazie per il vostro aiuto in anticipo, questo sito è sempre stato un tesoro e un grande aiuto.
Cheers, Simon
Si sta impostando il DataContext di UserControl ovunque? Sembra che le cose siano impostate correttamente, inclusa l'associazione, ma non è chiaro se si imposta effettivamente DataContext per mostrare il sistema da cui provengono effettivamente i valori. – Tim
Ciao @Tim, Nel fileView.xaml.cs ha una proprietà fileViewModel che restituisce DataContext come fileViewMode. L'interessante è aggiungendo DataContext alla griglia. Ha smesso di produrre un'eccezione. – sayo9394