c# - 根据数据库中的某些数据绑定listboxitem的背景颜色
问题描述
我正在尝试根据某些特定规则在 wpf 中为背景着色。我用绑定试过这个,但它不起作用。
我有 2 个区域和 16 个位置,如下所示:
A.ItemsSource = Enumerable.Range(1, 16);
B.ItemsSource = Enumerable.Range(1, 16);
在 backgroundWorker_DoWork 中,如果我有“打开”的东西,我会查看数据库,如果有,我会通过A.Dispatcher.BeginInvoke (System.Windows.Threading.DispatcherPriority.Normal, UpdateMyDelegate, mystring2, mystring1);
主线程发送 2 个代表区域和位置的字符串。
我有(在 MainWindow.cs 中):
private void UpdateMyDelegateListbox (int mystring2, string mystring1)
{
if (mystring1 == "A")
{
A.SelectedItem = mystring2;
VM.Background1 = something like new string () or bool somehow ??
}
else if (mystring1 == "B") ...
这个想法是检查哪个位置是开放的或“阻止”将其涂成红色
我只想绑定。我现在做的是这个(xaml):
<Style x:Key="SimpleListBoxItem" TargetType="ListBoxItem">
<!--<Setter Property="FocusVisualStyle" Value="{x:Null}"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<!--<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="Yellow"/>
</Trigger>-->
**<DataTrigger Binding="{Binding Success}" Value="True">
<Setter TargetName="Border" Property="Background" Value="Green"/>
</DataTrigger>**
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
- 每个区域都在堆栈面板中,并且都在 scrollViewer 中:
<!-- Area A -->
<ScrollViewer HorizontalScrollBarVisibility="Disabled" Grid.Row="1" VerticalScrollBarVisibility="Disabled" Height="500" VerticalAlignment="Top" Margin="0,10,0,-8" >
<WrapPanel Width="2000" Height="500">
<StackPanel Width="500" Height="500">
<ListBox x:Name="A" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Margin="10,10,85,85" SelectionMode="Multiple" ItemContainerStyle="{DynamicResource SimpleListBoxItem}" SelectionChanged="listboxA_SelectionChanged" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel ItemHeight="100"
ItemWidth="100"
Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="local:TestApp">
<StackPanel Margin="20" HorizontalAlignment="Center">
<Viewbox>
<Grid x:Name="backgroundGrid"
Width="48"
Height="48">
<Rectangle x:Name="Rect" Fill="Orange" />
<Label HorizontalContentAlignment="Center"
Content="{Binding}"
FontFamily="Segoe UI"
FontSize="24"
Foreground="White" />
</Grid>
</Viewbox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
在 ViewModel 我有:
class TestAppViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
private void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public object _background => null;
public string Background1
{
get
{
return Background1;
}
set
{
if (_background == value)
{
return;
}
Background1 = value;
RaisePropertyChanged("Background");
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Background1)));
}
}
}
我想使用像 IsOccupied 这样的东西来主要传输,如果在区域 y 的位置 x 有东西然后变成红色,否则留下橙色。我想实时看到谁忙谁空。
如果您能给我任何建议,我将不胜感激。谢谢你,对任何类型的错误感到抱歉,但我很着急发帖。:)
解决方案
您应该将 设置ItemsSource
为具有决定背景的属性的对象集合:
A.ItemsSource = Enumerable.Range(1, 16).Select(x => new TestAppViewModel(x));
向类添加一个int
属性TestAppViewModel
:
class TestAppViewModel : INotifyPropertyChanged
{
public TestAppViewModel(int number)
{
Number = number;
}
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
private void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public int Number { get; }
public object _background;
public string Background1
{
get
{
return Background1;
}
set
{
if (_background == value)
return;
_background = value;
RaisePropertyChanged("Background1");
}
}
}
并绑定到模板中的源属性:
<ListBox.ItemTemplate>
<DataTemplate DataType="local:TestApp">
<StackPanel Margin="20" HorizontalAlignment="Center">
<Viewbox>
<Grid x:Name="backgroundGrid"
Width="48"
Height="48">
<Rectangle x:Name="Rect" Fill="{Binding Background1}" />
<Label HorizontalContentAlignment="Center"
Content="{Binding Number}"
FontFamily="Segoe UI"
FontSize="24"
Foreground="White" />
</Grid>
</Viewbox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
推荐阅读
- html - 用 CSS替换图片
- java - 不兼容的类型:捕获 ? 扩展...不能转换为捕获?延伸
- c - 如何将数组存储在 MD 数组中,并从函数中返回该数组?
- java - 我用 Java 创建一个简单的服务器后 Telnet 无法连接到 localhost
- angular - 如何在继续之前使 Angular 拦截器延迟?
- angular - Ionic/Angular 是否具有集成的会话/令牌系统?
- swift - FirebaseStorage 3 中的 downloadURL 等效项是什么?
- sql - 将 ANY 与 OVER (PARTITION BY ...) 一起使用?
- integration - 使用 MapleSoft 解决集成
- java - 为什么第四行不以“Arnie”结尾?