当我在DataGrid上更新SelectedItem的属性时,我试图触发数据触发器。有人能告诉我我需要改变什么才能让它发挥作用吗?
当用户选择网格中的项目时,其Connected属性将设置为true。我在DataGrid中的标签应该从红色变为绿色。它保持红色。
我知道这种风格是有效的,因为如果我开始将属性Connected设置为true,它们都会变成绿色。当用户在DataGrid中选择一个项时,它似乎不会触发。
// Style set in Resources of my control
<Style x:Key="ConnectedStyle" TargetType="{x:Type Label}"
BasedOn="{StaticResource FontAwesome}">
<Setter Property="Foreground" Value="Red" /> // I added this last
minute thinking it might need to be initialized but still nothing.
<Style.Triggers>
<DataTrigger Binding="{Binding Connected}" Value="True">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Connected}" Value="False">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
<DataGrid Grid.Row="1" Grid.Column="1"
ItemsSource="{Binding AvailablePorts}"
SelectedItem="{Binding SelectedPort}"
HorizontalAlignment="Stretch"
SelectionMode="Single"
Style="{StaticResource PlainGrid}"
AutoGenerateColumns="False" ColumnHeaderHeight="30">
<DataGrid.Columns>
<DataGridTemplateColumn Header=" Status" Width="55">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Style="{StaticResource ConnectedStyle}"
Content="" FontSize="12" Margin="5,0,0,0"
Padding="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header=" Port" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Port}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
查看模型
public class EMSViewModel : ViewModelDetailBase<EMSViewModel, EMSModel>
{
public ObservableCollection<AvailablePortsModel> AvailablePorts
{
get
{
return this.availablePorts;
}
private set
{
this.availablePorts = value;
this.NotifyPropertyChanged(m => m.AvailablePorts);
}
}
public AvailablePortsModel SelectedPort
{
get
{
return this.selectedPort;
}
set
{
this.selectedPort = value;
this.NotifyPropertyChanged(m => m.SelectedPort);
this.ConnectToPort();
}
}
private void ConnectToPort()
{
// if the code (removed) evaluates to true, set the connected property to true.
this.SelectedPort.Connected = true;
this.NotifyPropertyChanged(m => m.AvailablePorts);
}
}
模型
namespace FastV.Models
{
using SimpleMvvmToolkit;
public class AvailablePortsModel : ModelBase<AvailablePortsModel>
{
private bool connected;
private string port;
public bool Connected
{
get
{
return this.connected;
}
set
{
this.connected = value;
this.NotifyPropertyChanged(m => m.Connected);
}
}
public string Port
{
get
{
return this.port;
}
set
{
this.port = value;
this.NotifyPropertyChanged(m => m.Port);
}
}
}
}