2014-10-27 96 views
0

首先,值得一提的是,我查看了其他類似的主題,他們幫助我獲得了這麼多,但我需要一點幫助才能完成終點線。WPF從DataTrigger更改圖像

我遇到的問題是,我不能讓我的DataTrigger顯示正確的圖像,當InPossesion布爾標誌設置爲false我將我的枚舉屬性設置爲IconImage2,這反過來應該改變數據網格中的圖像爲紅色圓圈,這不會發生。如果任何人都可以給我任何指點,說明我出錯的地方會很棒。

視圖模型枚舉

public enum IconEnum 
{ 
    IconImage1, 
    IconImage2 
} 

public IconEnum MyIconEnumProperty 
{ 
    get { return _myEnum; } 
    set 
    { 
     _myEnum = value; 
     RaisePropertyChanged("MyIconEnumProperty"); 
    } 
    }  

視圖模型方法加載訂單

private void LoadCloakroomOrders() 
{ 
    CloakroomOrderRepository repo = new CloakroomOrderRepository(); 
    //Get All orders 
    var orders = repo.GetPublic(); 
    foreach (var orderItem in orders) 
     { 
      Orders.Add(orderItem); 
      if (orderItem.InPossesion == false) 
      { 
       MyIconEnumProperty = IconEnum.IconImage2; 
      } 
     } 
    } 

XAML

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Orders}" 
        SelectedItem="{Binding Path=SelectedCloakroomOrder}" 
        Margin="0,23,0,-0.5" Width="980" > 
      <DataGrid.Columns> 
       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Image Visibility="{Binding ShowIcon, 
           Converter={StaticResource BooleanToVisibilityConverter}, 
           FallbackValue=hidden}" > 
           <Image.Style> 
            <Style TargetType="Image"> 
             <Setter Property="Source" Value="/Resources/Images/circle_green.png"/> 
             <Style.Triggers> 
              <DataTrigger Binding="{Binding MyIconEnumProperty}" Value="IconImage2"> 
               <Setter Property="Source" Value="/Resources/Images/circle_red.png"></Setter> 
              </DataTrigger> 
             </Style.Triggers> 
            </Style> 
           </Image.Style> 
          </Image> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 

謝謝!

+0

請發佈一個問題,解釋您遇到的問題,您只需要發佈要求... – 2014-10-27 14:18:27

+0

請勿爲視圖暴露'enum'。視圖模型可以使用enum(來自某些業務邏輯)的值,並將其轉換爲圖像源,您可以使用該圖像源來綁定視圖圖像。 – Sinatr 2014-10-27 14:51:56

回答

2

而不是在虛擬機中有一個枚舉,你可以簡單地有一個InPossesion屬性,並隱藏/顯示基於它的兩個圖像。這使視圖模型清潔和XAML更加清晰:

視圖模型:

public bool InPossession 
{ 
    get { return _inPossession; } 
    set { _inPossion = value; RaisePropertyChanged("InPossession"); } 
} 

private void LoadCloakroomOrders() 
{ 
    CloakroomOrderRepository repo = new CloakroomOrderRepository(); 
    //Get All orders 
    var orders = repo.GetPublic(); 
    foreach (var orderItem in orders) 
    { 
     Orders.Add(orderItem); 
     if (orderItem.InPossesion == false) 
     { 
      InPossession = false; 
     } 
    } 
} 

轉換器:

public class BooleanToVisibilityConverter : IValueConverter 
{ 
    public Visibility VisibilitIfTrue { get;set; } 
    public Visibility VisibilitIfFalse { get;set; } 

    public BooleanToVisibilityConverter() 
    { 
     // Set default values for the most common usage 
     VisibilityIfTrue = Visible; 
     VisibilityIfFalse = Collapsed; 
    } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 

    // Converter could be extended to handle nullable bools as well, but ignore for now 
    if (value == null) 
    { 
     return DependencyProperty.UnsetValue; 
    } 

    // value should be of type bool 
    bool b = (bool)value; 
    if (b == true) 
    { 
     return VisibilityIfTrue; 
    } 
    else 
    { 
     return VisibilityIfFalse; 
    } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); // Not necessary 
    } 
} 

XAML:

<UserControl> 
    <UserControl.Resources> 
     <converters:BooleanToVisibilityConverter x:Key="TrueToVisibleConverter" VisibilityIfTrue="Visible" VisibleIfFalse="Collapsed"/> 
     <converters:BooleanToVisibilityConverter x:Key="FalseToVisibleConverter" VisibilityIfTrue="Collapsed" VisibleIfFalse="Visible"/> 
    </UserControl.Resources> 
</UserControl> 
... 
<DataTemplate> 
    <Grid Visibility="{Binding ShowIcon, FallbackValue=hidden}"> 
     <Image Source="/Resources/Images/circle_green.png" Visibility="{Binding InPossession, Converter={StaticResource TrueToVisibleConverter}}"/> 
     <Image Source="/Resources/Images/circle_red.png" Visibility="{Binding InPossession, Converter={StaticResource FalseToVisibleConverter}}"/> 
    </Grid> 
</DataTemplate> 
+0

感謝你們,雖然我還沒有完全按照自己的需要進行排序。 – bucky112 2014-10-27 15:00:06

+0

我添加了代碼來說明我關於真/假可見性轉換器的想法 – sondergard 2014-10-27 15:19:28

+0

我在執行轉換器邏輯的新屬性時遇到困難我在看@ http://stackoverflow.com/questions/534575/how-do-i -invert-booleantovisibilityconverter但沒有多少運氣,你能提供一個例子嗎? – bucky112 2014-10-27 15:45:27

1

嘗試也指定枚舉類型。

Value="{x:Static wpf:IconEnum.IconImage2}" 

wpf:就像是在我的情況xmlns:wpf="clr-namespace:Sandbox.WPF"命名空間。但是我可能會選擇另一種解決方案,就像sondergard所說的那樣,它比這種黑客更加乾淨。