2017-06-13 68 views
0

我嘗試了不同的方法來解決問題。首先,我將ViewModel中的MainColor綁定爲String,Color,Brush,並嘗試將其綁定到邊框 - >不適合我。其次是一個轉換器,但我無法正確綁定字符串來激活轉換器。唯一能夠將MainColor作爲字符串綁定到Label風格的工具,我也使用了相同的資源。所以現在標籤中的變化起作用,但不在列表框中。在ListBoxItem中綁定BorderBackground

筆者認爲:

<Page.Resources> 
    <ResourceDictionary Source="/Resources/ResourcePageCuttingData.xaml"></ResourceDictionary> 
</Page.Resources> 
<ListBox ItemsSource="{Binding Material}" SelectedItem="{Binding SelectedMat, Mode=TwoWay}" Style="{StaticResource styleMat}" Grid.Column="5" Grid.Row="1" Margin="20,0"></ListBox> 
<Label Style="{StaticResource Label_Search}" Content="{DynamicResource so_lbl_cm}" ></Label> 

我在ResourceDictonary列表框:

<Style x:Key="styleMat" TargetType="{x:Type ListBox}"> 
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter> 
    <Setter Property="ItemContainerStyle" Value="{DynamicResource ResourceKey=styleLocMschItem}"></Setter> 
    <Setter Property="ItemTemplate" Value="{StaticResource ResourceKey=ResultMatDataTemplate}"></Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBox}"> 
       <Border BorderBrush="#5A5A5A" BorderThickness="1" CornerRadius="4"> 
        <ScrollViewer Margin="1"> 
         <ItemsPresenter Margin="1"></ItemsPresenter> 
        </ScrollViewer> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<DataTemplate x:Key="ResultMatDataTemplate"> 
    <TextBlock Text="{Binding}" 
        FontSize="20" 
       ></TextBlock> 
</DataTemplate> 

<Style x:Key="styleLocMachItem" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Grid x:Name="grid"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*"></RowDefinition> 
         <RowDefinition Height="Auto"></RowDefinition> 
        </Grid.RowDefinitions> 
        <Border x:Name="hover" 
         Background="YellowGreen" 
         Visibility="Collapsed"> 
        </Border> 
        <Border x:Name="orginal" 
         Background="{Binding MainColor}"> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter TargetName="highlight" 
          Property="Visibility" 
          Value="Visible"> 
         </Setter> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

的MainColor沒有在邊境X工作:NAME = 「原創」

我標籤:

<Style x:Key="Label_Search" TargetType="{x:Type Control}"> 
    <Setter Property="Background" Value="{Binding MainColor}" /> 
</Style> 

這裏MainColor工作正常

我的視圖模型:

public CuttingSpeed_ViewModel() 
    { 
     switch (value) 
     { 
      case "Fast": 
       MainColor = "Pink"; 
        break;   
      case "Slow": 
       MainColor = "Yellow"; 
        break; 
      default: 
        MainColor ="Red"; 
       break; 
     }} 
    private string _MainColor; 

    public string MainColor 
    { 
     get { return _MainColor; } 
     set { _MainColor = value; OnPropertyChanged("MainColor"); } 
    } 
    public IEnumerable<string> Material 
    { 
     get { return _Material; } 
     set { _Material = value; OnPropertyChanged("Material"); } 
    } 
    private string _SelectedMat; 

    public string SelectedMat 
    { 
     get { return _SelectedMat; } 

     set 
     {..}} 

我覺得應該有東西在XAML不允許我獲得價值MainColor。

回答

1

MainColor屬性應該返回Brush

public CuttingSpeed_ViewModel() 
{ 
    switch (value) 
    { 
     case "Fast": 
      MainColor = Brushes.Pink; 
      break; 
     case "Slow": 
      MainColor = Brushes.Yellow; 
      break; 
     default: 
      MainColor = Brushes.Red; 
      break; 
    } 
} 

private Brush _MainColor; 
public Brush MainColor 
{ 
    get { return _MainColor; } 
    set { _MainColor = value; OnPropertyChanged("MainColor"); } 
} 

你也應該綁定到父ListBoxDataContext

<Border x:Name="orginal" Background="{Binding DataContext.MainColor, 
     RelativeSource={RelativeSource AncestorType=ListBox}}"> 
+0

它工作正常,謝謝版本了。 – X89i

相關問題