c# - 命令执行后转换器不会被调用
问题描述
这个按钮应该做的是循环浏览可用的颜色列表并相应地更改背景颜色。
最初,我将这部分视图模型代码放在一个大/主视图模型中,但决定将其拆分,以便我的每个用户控件都有自己的视图模型。我尝试在 OnChangeColor() 方法执行的地方设置一个断点,我看到 State 确实发生了变化,但它似乎没有将这种更改持续到 Converter 中,因此根本不会更新 UI 的背景颜色。
我在下面发布了代码,我已经对其进行了编辑,目前正在尝试找出问题所在。
主窗口
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type local:CameraListViewModel}">
<local:MainView />
</DataTemplate>
<DataTemplate DataType="{x:Type rep:ReportViewModel}">
<rep:ReportView />
</DataTemplate>
<DataTemplate DataType="{x:Type cam:CameraMonitorViewModel}">
<cam:CameraMonitorView />
</DataTemplate>
<DataTemplate DataType="{x:Type cam:CameraPropertiesViewModel}">
<cam:CameraPropertiesView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding CurrentViewModel}" />
</Grid>
查看型号:
public class CameraPropertiesViewModel : ViewModelBase
{
/// <summary>
/// The current state of the background color
/// </summary>
private States _state;
//Delegate commands for the UI
private DelegateCommand _changeColorCommand;
public ICommand ChangeColorCommand
{
get
{
if (_changeColorCommand == null)
{
_changeColorCommand = new DelegateCommand(OnChangeColor);
}
return _changeColorCommand;
}
}
//All possible background colors
public enum States
{
GREEN,
YELLOW,
RED
}
//Gets or sets the background color
public States State
{
get
{
return _state;
}
set
{
SetProperty(ref _state, value);
}
}
//Cycle through different colors
private void OnChangeColor()
{
if (State == States.GREEN)
State = States.RED;
else if (State == States.RED)
State = States.YELLOW;
else if (State == States.YELLOW)
State = States.GREEN;
}
}
转换器:
[ValueConversion(typeof(CameraPropertiesViewModel.States), typeof(Brush))]
public class EnumToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CameraPropertiesViewModel.States state = (CameraPropertiesViewModel.States)value;
switch (state)
{
case CameraPropertiesViewModel.States.GREEN:
return new SolidColorBrush(Colors.Green);
case CameraPropertiesViewModel.States.YELLOW:
return new SolidColorBrush(Colors.Yellow);
case CameraPropertiesViewModel.States.RED:
return new SolidColorBrush(Colors.Red);
}
return new SolidColorBrush(Colors.LightGray);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
主视图
<UserControl.Resources>
<conv:EnumToColorConverter x:Key="enumToColorConvert"/>
<cam:CameraPropertiesViewModel x:Key="CPVM" />
</UserControl.Resources>
<Grid Background="{Binding Path=State, Source={StaticResource CPVM}, Converter={StaticResource enumToColorConvert}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<cam:CamerasListView />
<cam:CameraPropertiesView Grid.Column="1"/>
<cam:CameraMonitorView Grid.Row="1"
Grid.ColumnSpan="2" />
<cam:CameraFunctionView Grid.Row="2"
Grid.ColumnSpan="2"/>
</Grid>
资源:
<local:CameraPropertiesViewModel x:Key="CPVM" />
按钮
<Button Style="{StaticResource circleButton}"
Command="{Binding ChangeColorCommand, Source={StaticResource CPVM}}"
Content="Change Theme"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="2"
Grid.Row="2"/>
CameraPropertiesView.xaml
<UserControl.Resources>
<local:CameraPropertiesViewModel x:Key="CPVM" />
<Style TargetType="{x:Type Button}" x:Key="circleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Viewbox>
<Canvas Width="50" Height="50">
<Ellipse Fill="{TemplateBinding Control.Background}" Width="50" Height="50"/>
</Canvas>
</Viewbox>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<GroupBox Header="Camera Details"
Height="130"
Width="386"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Margin="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>
<Label Height="25"
Width="70"
Content="Name"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Right" />
<TextBox Text="{Binding ConnectedCamera.Name}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="23"
Width="160"
Grid.Column="1"
IsReadOnly="True" />
<Label Height="25"
Width="70"
Content="Model"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="1"
HorizontalContentAlignment="Right" />
<TextBox Text="{Binding ConnectedCamera.Model}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="23"
Width="160"
IsReadOnly="True"
Grid.Column="1"
Grid.Row="1" />
<Label Height="25"
Width="70"
Content="Ip Address"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="2"
HorizontalContentAlignment="Right" />
<TextBox Text="{Binding ConnectedCamera.IpAddress}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="23"
Width="160"
IsReadOnly="True"
Grid.Column="1"
Grid.Row="2" />
<Button Command="{Binding GenerateReportCommand}"
Content="Generate Report"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="2" />
<Button Command="{Binding ArchiveReportCommand}"
Content="Archive Report"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="2"
Grid.Row="1"/>
<Button Style="{StaticResource circleButton}"
Command="{Binding ChangeColorCommand, Source={StaticResource CPVM}}"
Content="Change Theme"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="2"
Grid.Row="2"/>
</Grid>
</GroupBox>
</Grid>
解决方案
通过去除不重要的样式、视图模型等,我能够重现您的问题。
您只是缺少网格绑定中的 Source 属性。
注意你是如何为按钮而不是网格拥有它的。
<Grid Background="{Binding Path=State,
Converter={StaticResource enumToColorConvert}}">
需要是:
<Grid Background="{Binding Path=State,
Converter={StaticResource enumToColorConvert},
Source={StaticResource CPVM}}">
推荐阅读
- laravel - 文件上传存储()不返回路径 Laravel
- android - 为什么应用内升级不起作用?为什么它不使用 onSuccss() 方法?
- python - 在 Python 2 和 Python 3 中匹配 Unicode 字符
- jquery - Show modal after clicking on a photo taken from external API
- spring-boot - Donot wrap exceptions of Feign client Fall back method with Hystrix RunTime Exception
- google-apps-script - Paste multiple rows and generate ID for them
- python-3.x - 无法使用 quickfix 与 FIX 协议连接
- uiviewcontroller - UINavigationBar 或 statusBarHeight -> 0 当 CGAffineTransform 应用于 viewController.view
- apache-poi - 如何使用 OpenCSV 的 CSVReader、CSVWriter 读取、写入 XLSX 文件?可能吗?
- php - 无法获取通过提交处理程序中的 ajax 提交的表单文本字段的值