2017-03-16 78 views
2

我有一個聯繫人列表,我想要顯示字母表並將沒有對應聯繫人的字母灰掉。更改按鈕的前景色

在我的模型,我有:

public class ContactInitial 
{ 
    public string Initial { get; set; } 
} 

這是從數據庫導出。

在我看來,我有以下

<StackPanel Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Width="17"> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">A</Button > 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">B</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">C</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">D</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">E</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">F</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">G</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">H</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">I</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">J</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">K</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">L</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">M</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">N</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">O</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">P</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">Q</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">R</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">S</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">T</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">U</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">V</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">W</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">X</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">Y</Button> 
    <Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">Z</Button> 
</StackPanel> 

我奮力摸出我如何綁定前景色爲灰色,如果這封信是不是在ContactInitial和黑色的,如果它是。

感謝您的幫助

回答

2

我會用一種風格,你可以把你的窗口,或者你的容器控制的資源實現它,爲的風格會在窗口/控制應用到所有按鈕。

在這種風格中,您可以放置​​一個觸發器,如果​​條件爲真,觸發器將觸發。我將檢查一個多值轉換器中的條件,該值應該用於綁定您查看按鈕的模型屬性和Content屬性。

這一個應該工作:

<Window.DataContext> 
     <local:ViewModel ></local:ViewModel> 
    </Window.DataContext> 
    <Window.Resources> 
     <local:ContainsStringConverter x:Key="strContConv"/> 
     <Style TargetType="Button"> 
      <Setter Property="Foreground" Value="Gray"/> 
      <Style.Triggers> 
       <DataTrigger Value="true"> 
        <DataTrigger.Binding> 
         <MultiBinding Converter="{StaticResource strContConv}"> 
          <Binding Path="InitialString"/> 
          <Binding RelativeSource="{RelativeSource Self}" Path="Content"/> 
         </MultiBinding> 
        </DataTrigger.Binding> 
        <Setter Property="Foreground" Value="Black"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

using System; 
    using System.Globalization; 
    using System.Linq; 
    using System.Windows.Data; 
    public class ContainsStringConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (values==null || values.Length<2 || !values.All(v=>v is string)) 
      { 
       return false; 
      } 
      var retVal = true; 
      var initalStr = values[0] as string; 
      for (int i = 1; i < values.Length; i++) 
      { 
       retVal &= initalStr.Contains(values[i] as string); 
      } 
      return retVal; 
     } 
     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

public class ViewModel : ViewModelBase 
{ 
    public ViewModel() 
    { 
     _initialString = Model.Initial; 
    } 
    public ContactInitial Model { get; set; } = new ContactInitial { Initial = "AC" }; 

    public string InitialString 
    { 
     get 
     { 
      return _initialString; 
     } 
     set 
     { 
      if (value != _initialString) 
      { 
       _initialString = value; 
       Model.Initial = _initialString; 
       NotifyPropertyChanged(nameof(InitialString)); 
      } 
     } 
    } 
    private string _initialString; 
} 
+0

嗨,特里薩,我已經修改了代碼,讓你有Model屬性 – Rekshino

+0

謝謝你,我做了一個稍微不同的方式,以便在構造函數中,我把下列: _initialString = CurrentContact.Initials; –