我正在为理论上的类开发一个图灵机模拟器,我正在尝试根据机器是否接受该语言来改变输入区域的背景颜色(基本上,一种颜色超过另一种颜色取决于如果它是有效的输入。)
由于我想提供几个示例输入,它需要是一个ComboBox。由于教授需要测试自己的输入,因此它也必须是可编辑的。所以,我们在这里。
我尝试过以编程方式设置ComboBox.Background
属性并使用XAML(通过Property编辑器),但都不起作用。但是,设置ComboBox.Foreground
没问题。
这是我的XAML:
<Window x:Class="Turing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Turing Machine Emulator" Height="400" Width="600" Loaded="onload" MinHeight="500" MinWidth="600">
<Grid>
<ComboBox x:Name="drpProblem" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="changeproblem"/>
<Label x:Name="lblDescription" Content="Language Description" Margin="135,7,90,0" VerticalAlignment="Top"/>
<Grid Margin="10,0,10,35" Height="24" VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="10*"/>
</Grid.ColumnDefinitions>
<Label x:Name="lblLeft" Content="left" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="0" Margin="0,0,10,0" FontFamily="Consolas"/>
<Label x:Name="lblRight" Content="right" Grid.Column="2" VerticalContentAlignment="Center" Padding="0" Margin="10,0,0,0" FontFamily="Consolas"/>
<Label x:Name="lblCenter" Content="cur" Grid.Column="1" Height="24" Padding="0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="#FFC5FFA4" FontFamily="Consolas" FontSize="16"/>
</Grid>
<Button x:Name="btnIterate" Content="Iterate" Margin="10,0,0,64" Height="20" VerticalAlignment="Bottom" Click="btnIterate_Click" HorizontalAlignment="Left" Width="236"/>
<!-- This one right here -->
<ComboBox x:Name="txtInput" Height="23" Margin="10,0,10,89" Text="Input String" VerticalAlignment="Bottom" FontFamily="Consolas" VerticalContentAlignment="Center" TextBoxBase.TextChanged="cboGetInput" BorderBrush="{x:Null}" Foreground="Black" Background="#FF874343" IsEditable="True" />
<TextBox x:Name="txtMs" Height="20" Margin="251,0,172,64" TextWrapping="Wrap" Text="wait (seconds)" VerticalAlignment="Bottom"/>
<Button x:Name="btnAutoRun" Content="AutoRun" Margin="0,0,10,64" Click="btnAutoRun_Click" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="157"/>
<TextBox x:Name="txtTM" Margin="10,38,10,142" TextWrapping="Wrap" Text="Language" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" FontFamily="Consolas" FontSize="14"/>
<Button x:Name="btnLoadLang" Content="Load" Margin="10,0,10,117" Height="20" VerticalAlignment="Bottom" Click="changeproblem"/>
<StatusBar Height="30" VerticalAlignment="Bottom">
<TextBlock x:Name="stTXTName" Text="StateName"/>
<Separator/>
<TextBlock x:Name="stTXTDescription" Text="StateDescription"/>
<Separator/>
<TextBlock x:Name="stTXTTransition" Text="NextTransition"/>
<Separator/>
<TextBlock x:Name="stTXTNext" Text="NextState"/>
</StatusBar>
</Grid>
</Window>
这是我用来尝试改变颜色的代码:
if (TM.AcceptsString(txtInput.Text))
{
txtInput.Background = Brushes.LightGreen;
txtInput.Foreground = Brushes.LightGreen;
}
else
{
txtInput.Background = Brushes.Pink;
txtInput.Foreground = Brushes.Pink;
}
前景按预期更改,但背景颜色永远不会更改为默认白色。难道我做错了什么?我需要在ComboBox
中设置一些组件控件来设置属性,就像我使用TextBoxBase.TextChanged
一样吗?
答案 0 :(得分:0)
将ComboBox的FlatStyle属性设置为FlatStyle.Flat。这解决了我在打开Win 7 Aero主题时遇到的类似问题:ComboBox的背景颜色未显示在默认的FlatStyle设置FlatStyle.Standard中。
答案 1 :(得分:0)
我有一个类似的问题,并且能够通过在Visual Studio 2015中自动生成ComboBox的模板来解决它(在设计窗口中右键单击ComboBox - &gt;编辑模板 - &gt;编辑副本)