gpt4 book ai didi

xaml - 更改按钮的前景色

转载 作者:行者123 更新时间:2023-12-03 10:16:04 25 4
gpt4 key购买 nike

我有一个联系人列表,我想显示字母表并将没有相应联系人的字母变灰。

在我的模型中,我有:

public class ContactInitial
{
public string Initial { get; set; }
}

这是从数据库中得出的。

在我看来,我有以下内容
<StackPanel Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left"  Width="17">
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">A</Button >
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">B</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">C</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">D</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">E</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">F</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">G</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">H</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">I</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">J</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">K</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">L</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">M</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">N</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">O</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">P</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">Q</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">R</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">S</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">T</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">U</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">V</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">W</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">X</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">Y</Button>
<Button Height="17" Background="#FFFBF5A6" BorderBrush="{x:Null}">Z</Button>
</StackPanel>

如果字母不在 ContactInitial 中,我正在努力弄清楚如何将前景色绑定(bind)为灰色,如果是,则将其绑定(bind)为黑色。

谢谢你的帮助

最佳答案

我将使用一种样式来实现它,您可以将其放入窗口或容器控件的资源中,因为该样式将应用于窗口/控件中的所有按钮。

在这种风格中,您可以放置​​一个触发器,如果​​条件为真,它将触发。我将在多值转换器中检查的条件,应该在您的 View 模型属性和按钮的内容属性的绑定(bind)中使用。

这个应该工作:

   <Window.DataContext>
<local:ViewModel ></local:ViewModel>
</Window.DataContext>
<Window.Resources>
<local:ContainsStringConverter x:Key="strContConv"/>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Gray"/>
<Style.Triggers>
<DataTrigger Value="true">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource strContConv}">
<Binding Path="InitialString"/>
<Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>

using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
public class ContainsStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values==null || values.Length<2 || !values.All(v=>v is string))
{
return false;
}
var retVal = true;
var initalStr = values[0] as string;
for (int i = 1; i < values.Length; i++)
{
retVal &= initalStr.Contains(values[i] as string);
}
return retVal;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

public class ViewModel : ViewModelBase
{
public ViewModel()
{
_initialString = Model.Initial;
}
public ContactInitial Model { get; set; } = new ContactInitial { Initial = "AC" };

public string InitialString
{
get
{
return _initialString;
}
set
{
if (value != _initialString)
{
_initialString = value;
Model.Initial = _initialString;
NotifyPropertyChanged(nameof(InitialString));
}
}
}
private string _initialString;
}

关于xaml - 更改按钮的前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42839055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com