0
編輯:我在後臺,我的XAML改變了一些東西,但我沒有收到任何的我的風格..綁定功能,DataTrigger WPF
<DataGrid
AutoGenerateColumns="False" Height="Auto"
Grid.Row="2" Grid.RowSpan="5"
HorizontalAlignment="Left"
VerticalAlignment="Top" Width="Auto"
Tag="Text"
ItemsSource="{Binding Nodes}"
IsSynchronizedWithCurrentItem="True"
x:Name="NodeDataGrid" Background="{Binding Color}">
<DataGrid.Columns>
<DataGridTextColumn Header="Node ID" Binding="{Binding NodeID}"></DataGridTextColumn>
<DataGridTextColumn Header="Status" Binding="{Binding Status}"></DataGridTextColumn>
<DataGridTextColumn Header="Temp" Binding="{Binding Temperature}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<!--<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>-->
<Style.Triggers>
<!--<DataTrigger Binding="{Binding Path=NodeColor, RelativeSource={RelativeSource Self}, Converter={StaticResource Conversion.RangeToBoolean}}" Value="True">
<Setter Property="Background" Value="LightBlue"/>
</DataTrigger>-->
<DataTrigger Binding="{Binding NodeColor}" Value="1">
<Setter Property="TextBlock.Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Damp" Binding="{Binding Dampness}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
而在另一個類..
public class RangeToBoolean : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((double)value) < 0.5;
}
}
在我NodeColorViewModel
private List<NodeColor> DetermineColor(List<NodeItem> nodes, List<NodeColor> nodeColor)
{
//SolidColorBrush myBrush = new SolidColorBrush(Colors.White);
foreach (var item in nodes)
{
NodeColor aNode = new NodeColor();
aNode.Color = 0;
if (item.Temperature >= 80.0)
{
//aNode.Color = new SolidColorBrush(Colors.LightSalmon);
aNode.Color = 1;
}
else if (item.Temperature < 20.0)
{
//aNode.Color = new SolidColorBrush(Colors.LightBlue);
aNode.Color = 2;
}
nodeColor.Add(aNode);
}
return nodeColor;
}
}
在我MainWindowViewModel
public MainWindowViewModel()
{
nodes = DetermineStatus(nodes);
var colors = DetermineColor(nodes, nodeColor);
var nodeColorViewModel = new List<NodeColorViewModel>();
foreach (var item in colors)
{
//nodes.ForEach(c => nodeViewModel.Add(new NodeItemViewModel(c)));
nodeColorViewModel.Add(new NodeColorViewModel(item));
}
NodeColor = nodeColorViewModel;
}
我的目標是隻顯示一定的紅色背景(溫度高於80度),請讓我知道你是否可以想到這個問題。現在,只顯示默認樣式,幾乎就像我的觸發器正在啓動。
這是編譯嗎?根據您的代碼段,IValueConverter界面不完整。你不想聲明你的轉換器是一個靜態資源,所以它可以用於綁定? –
好吧,我在我的RangeToBoolean類中有另一個類 public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture) {0} } 其他建議? –