2011-03-17 43 views
0

我有一個由四個重疊項目的用戶控件:2米的長方形,橢圓形和拉布勒更改用戶控件的外觀基於狀態

<UserControl x:Class="UserControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="50.1" Height="45.424" Background="Transparent" FontSize="24"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="3.303*" /> 
     <RowDefinition Height="40*" /> 
     <RowDefinition Height="2.121*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="5.344*" /> 
     <ColumnDefinition Width="40.075*" /> 
     <ColumnDefinition Width="4.663*" /> 
    </Grid.ColumnDefinitions> 
    <Rectangle Name="Rectangle1" RadiusX="5" RadiusY="5" Fill="DarkGray" Grid.ColumnSpan="3" Grid.RowSpan="3" /> 
    <Ellipse Name="ellipse1" Fill="{Binding State}" Margin="0.016,0.001,4.663,0" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Stroke="Black" IsEnabled="True" Panel.ZIndex="2" /> 
    <Label Name="lblNumber" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="White" FontWeight="Bold" FontSize="24" Grid.Column="1" Grid.Row="1" Padding="0" Panel.ZIndex="3">9</Label> 
    <Rectangle Grid.Column="1" Grid.Row="1" Margin="0.091,0,4.663,0" Fill="Blue" Name="rectangle2" Stroke="Black" Grid.ColumnSpan="2" Panel.ZIndex="1" /> 
</Grid> 

這是我的目標,我想控制我的用戶控件的狀態:

Imports System.Data 
Imports System.ComponentModel 

Public Class BusinessObject 
    Implements INotifyPropertyChanged 
    Public logger As log4net.ILog 

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged 

    Private _state As States 
    Public Enum States 
     State1 
     State2 
     State3 
    End Enum 

    Public Property State() As States 
     Get 
      Return _state 
     End Get 
     Set(ByVal value As States) 
      If (value <> _state) Then 
       _state = value 
       RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("State")) 
      End If 

     End Set 
    End Property 

    Protected Sub OnPropertyChanged(ByVal name As String) 
     RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name)) 
    End Sub 

我希望能夠改變一個業務對象的狀態,在後面的代碼,並有更改多個圖形的顏色在我的用戶。我不確定如何做綁定。我在代碼背後設置了用戶控件的datacontext,但不確定是否正確。我對WPF和編程一般都很陌生,而且我堅持要從這裏開始。任何建議將不勝感激!

回答

0

您可以根據State屬性創建一個觸發器,如果​​它等於StateX,則會更改顏色。例如:

<Rectangle> 
    <Rectangle.Style> 
     <Style TargetType="{x:Type Rectangle}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding State} " 
          Value="{x:Static localNamespace:States.State1}"> 
        <Setter Property="Fill" Value="Red" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Rectangle.Style> 
</Rectangle> 

localNamespace你必須定義自己在<UserControl>標籤。像<UserControl xmlns:localNamespace="clr-namespace:MyNamespace.MyClassWithStateEnum;assembly=MyNamespace"

+0

感謝您的回覆。我喜歡這種方法。但我有一個問題聲明本地命名空間。我的解決方案被命名爲'Application1'。所以我將localnamespace聲明爲xmlns:localNamespace =「clr-namespace:Application1」。它不會讓我將其聲明爲的xmlns:localNamespace =「CLR的命名空間:Application1.BusinessObject」它說,它不包含在裝配 – John 2011-03-17 16:41:59

+0

@約翰這很好,只是改變你的'X:Static'到'localNamespace:BusinessObject的.States.State1' – Rachel 2011-03-17 17:03:49

1

一個簡單的方法是使用一個值轉換器。基本上,這是一個允許綁定BusinessObject中的值的類,根據值的含義,可以返回不同的畫刷。

Here是一個向您顯示如何操作的示例。

[ValueConversion(typeof(States), typeof(Brush))] 
public class ColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
    /* return a different brush depending on the state */ 
    } 
} 

然後綁定這樣的:

<Ellipse Fill="{Binding State, Converter={StaticResource colorConverter} /> 

看鏈接我提供上面看到完整的例子。

這種方式優於Rachel的答案是它是一個通用的實現,所以如果你想讓這個模板適用於不同的對象(矩形,橢圓,等等...)。但是Rachel的回答 - 即使用模板,也很好,因爲它不需要任何代碼。

相關問題