在WPF項目,我具有限定一個多邊形形狀的用戶控件(Valve.xaml)。如何動態地設置的元件的樣式在一用戶控制
<Grid>
<Polygon Name="pValve" Points="0,50 0,20 50,50 50,20" Style="{StaticResource Valve_Open}"/>
</Grid>
我顯示在一個窗口XAML(FFG.xaml)文件中的閥門的用戶控制,像這樣:
<Window
<!-- removed other namespaces for brevity -->
xmlns:cl="clr-namespace:FFG.Controls;assembly=PID.Controls">
<Grid>
<cl:Valve x:Name="valve201A"></cl:Valve>
</Grid>
</Window>
我設置FFG.xaml的DataContext的類FFG_ViewModel.cs,並且它包含Valve_Model類的一個實例。 Valve_Model基本上代表FFG.xaml窗口上繪製的閥門。
public class FFG_ViewModel : ViewModelBase {
public Valve_Model Valve201A { get; set; }
// There are other properties and methods, but I've removed them for brevity also
}
這裏是Valve_Model類:
public class Valve_Model INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private bool _isValveOpen { get; set; }
public bool IsValveOpen {
get {
return _isValveOpen;
}
set {
_isValveOpen = value;
OnPropertyChanged("IsValveOpen");
}
}
#region INotifyPropertyChanged
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null) {
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
#endregion
}
問題: 我想是在Valve.xaml樣式屬性來更改IsValveOpen屬性發生變化時。
因此,如果閥門打開那麼這將是:
<Polygon Name="pValve" Points="0,50 0,20 50,50 50,20" Style="{StaticResource Valve_Open}"/>
當屬性更改爲false,那麼我需要的多邊形的風格改爲:
<Polygon Name="pValve" Points="0,50 0,20 50,50 50,20" Style="{StaticResource Valve_Closed}"/>
我該如何去實現這一點?
這樣做了。謝謝您的幫助! – Jagd