如何将 textblock.foreground 绑定到变量?(WPF C#)

Stu*_*nna 3 c# wpf textblock visual-studio-2013

所以我希望改变我的程序,以便我可以运行一个函数来检查前景色是否应该是黑色或银色。我希望将“不可访问”的字段涂灰。

我的表格目前看起来像: 在此处输入图片说明

我希望将“无需维护”字段“变灰”。但是我在尝试为数据模板中的字体前景定义绑定元素时遇到问题。

我已经尝试了从尝试在主窗口代码中定义 IValueConverter 类到定义窗口键资源的所有方法,但似乎我无法在文本块元素本身的数据模板中执行此操作?

任何建议/帮助将不胜感激。谢谢!

XAML:

<Grid Margin="0,0,2,0">

    <ListBox x:Name="allSites_LB" 
             HorizontalAlignment="Left" 
             Height="400" 
             Margin="20,60,0,0" 
             VerticalAlignment="Top" 
             Width="945"
             Loaded="allSites_LB_Loaded" 
             BorderThickness="1" SelectionChanged="allSites_LB_SelectionChanged"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             >
        <ListBox.ItemTemplate >
            <DataTemplate >
                <Border BorderBrush="Black" BorderThickness="0,0,0,1" Margin="-20,1,0,1" Padding="0,5,0,5" >
                    <Grid Margin="75,3" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200" />
                            <ColumnDefinition Width="400" />
                            <ColumnDefinition Width="345" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding SiteNo}" Grid.Column="0" FontSize="16" />
                        <TextBlock Text="{Binding Address}" Grid.Column="1" FontSize="16" Margin="50,1" />
                        <TextBlock Text="{Binding MaintStatus}" Grid.Column="2" FontSize="16" />
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <Button x:Name="viewHistory_BTN" 
            Content="View History" 
            HorizontalAlignment="Left" 
            Height="52" 
            Margin="20,496,0,0" 
            VerticalAlignment="Top" 
            Width="172" FontSize="20"
            />

    <Button x:Name="startMaintenance_BTN" 
            Content="Start Maintenance"
            HorizontalAlignment="Left" 
            Height="52" 
            Margin="793,496,0,0" 
            VerticalAlignment="Top" 
            Width="172" FontSize="20"
            />
    <TextBox x:Name="Site_Address" 
             HorizontalAlignment="Left" 
             Height="21" 
             Margin="51,39,0,0" 
             TextWrapping="Wrap" 
             Text="Site Number" 
             VerticalAlignment="Top" 
             Width="75" 
             BorderBrush="White" 
             IsReadOnly="True" 
             IsEnabled="False"

             />
    <TextBox x:Name="Address_Title" 
             HorizontalAlignment="Left" 
             Height="21" 
             Margin="380,34,0,0" 
             TextWrapping="Wrap" 
             Text="Address" 
             VerticalAlignment="Top" 
             Width="75" 
             BorderBrush="White"
             IsReadOnly="True" 
             IsEnabled="False"

             />
    <TextBox x:Name="maint_Title" 
             HorizontalAlignment="Left" 
             Height="21" 
             Margin="699,34,0,0" 
             TextWrapping="Wrap" 
             Text="Maintenance Record" 
             VerticalAlignment="Top" 
             Width="117" 
             BorderBrush="White" 
             IsReadOnly="True" 
             IsEnabled="False"
             />

</Grid>
Run Code Online (Sandbox Code Playgroud)

背后的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;



namespace SiteMaintenance
{

public partial class MainWindow : Window
{

    /**
     * CLASS VARIABLES
     * */
    private SqlConnection localdbConnection;        // Connection to Site Maintenance DB (LOCAL)
    private System.Data.DataSet allSitesResults;



    // MAIN THREAD
    public MainWindow()
    {
        InitializeComponent();

        // try to open SQL Connection
        try {
            localdbConnection = new SqlConnection(Properties.Settings.Default.localdb);
            localdbConnection.Open();
        } catch(Exception ex) {
           System.Windows.MessageBox.Show("local SQL connection unable to connect");
           return;
        }

        viewHistory_BTN.IsEnabled = false;
        startMaintenance_BTN.IsEnabled = false;
        startMaintenance_BTN.IsDefault = true;
    }

    /**
     * Load dataset into datagrid 
     * LAZY LOADING
     * */
    private void DataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        // init command object
        SqlCommand myCommand = new SqlCommand();
        myCommand.CommandText = "dbo.usp_GetSites";
        myCommand.CommandType = System.Data.CommandType.StoredProcedure;
        myCommand.Connection = localdbConnection;

        // init data adaptor
        SqlDataAdapter sites = new SqlDataAdapter();
        sites.SelectCommand = myCommand;

        //init DataSet
        allSitesResults = new System.Data.DataSet();

        sites.Fill(allSitesResults, "tblSites");

        int tableCount = allSitesResults.Tables.Count;

        System.Data.DataTable test = allSitesResults.Tables[0];

        int rowCount = test.Rows.Count;





    }

    private void sites_DG_CurrentCellChanged(object sender, EventArgs e)
    {
        String siteName = allSitesResults.Tables[0].Rows[0][1].ToString();

    }

    private void allSites_LB_Loaded(object sender, RoutedEventArgs e)
    {
        // init command object
        SqlCommand myCommand = new SqlCommand();
        myCommand.CommandText = "dbo.usp_GetSitesANDCompletedDate";
        myCommand.CommandType = System.Data.CommandType.StoredProcedure;
        myCommand.Connection = localdbConnection;

        // init data adaptor
        SqlDataAdapter sites = new SqlDataAdapter();
        sites.SelectCommand = myCommand;

        //init DataSet
        allSitesResults = new System.Data.DataSet();

        sites.Fill(allSitesResults, "tblSites");

        allSites_LB.ItemsSource = allSitesResults.Tables["tblSites"].DefaultView;

    }


    // do not allow selection of maintenance records that do not exist
    private void allSites_LB_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        // grab the index
        int selectedIndex = allSites_LB.SelectedIndex;
        if (selectedIndex == -1) return;                //WITHOUT THIS CHECK, UN-SELECTION WILL CAUSE LOGIC FAILURE

        System.Data.DataRowView tempData = (System.Data.DataRowView)allSites_LB.Items[allSites_LB.SelectedIndex];

        // grab the completed date field
        String completedDate = tempData["CompletedDate"].ToString();
        String siteMaintID = tempData["SiteMaintID"].ToString();

        // remove selected index if completed date and site Maint ID is null
        if (siteMaintID != "" && completedDate == "")
        {
            startMaintenance_BTN.IsEnabled = true;
        }
        else 
        {
            allSites_LB.SelectedIndex = -1;
            startMaintenance_BTN.IsEnabled = false;
        }

    }


    private String maintRequired(object sender, SelectionChangedEventArgs e)
    {
        int selectedIndex = allSites_LB.SelectedIndex;
        if (selectedIndex < 0) return null;

        System.Data.DataRowView tempData = (System.Data.DataRowView)allSites_LB.Items[allSites_LB.SelectedIndex];

        // grab the completed date field
        String completedDate = tempData["CompletedDate"].ToString();
        String siteMaintID = tempData["SiteMaintID"].ToString();

        if (siteMaintID != "" && completedDate == "")
        {
            return "Maintenance Required";
        }
        else
        {
            return "No Maintenance";
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

}

BTo*_*TKD 5

在将前景色绑定到一段数据时,通常有两种好方法供您选择。根据您询问的对象,不同的人会有不同的偏好。所以……两者都有!

第一种方法:带触发器的样式

当满足一组特定条件时,此方法基本上可以识别“特殊”行为。在这种情况下,当状态 ==“无需维护”时,我们将前景色更改为灰色

<Style TargetType="TextBlock">
    <Setter Property="Foreground" Value="Black" /> <!-- default value -->
    <Style.Triggers>
        <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}" Value="No Maintenance Required">
            <Setter Property="Foreground" Value="Gray" /> <!-- special behavior -->
        </DataTrigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,只需为您的 TextBlock 分配适当的Style属性。

第二种方法:使用 IValueConverter

这种方法创建了一个自定义的“IValueConverter 实现,它将您的文本值转换为颜色。从那里,我们直接绑定到我们的文本值,并确保转换器始终提供正确的颜色。

public class MaintenaceColorConverter : IValueConverter
{

    public Color NormalColor { get; set; }
    public Color NoMaintenanceRequiredColor { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value.ToString() == "No Maintenance Required")
            return NoMaintenanceRequiredColor;

        return NormalColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的 XAML 中:

<Window.Resources>
    <local:MaintenaceColorConverter x:Key="myColorConverter" NormalColor="Black" NoMaintenanceRequiredColor="Gray" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

在您的文本块中:

<TextBlock Text="{Binding MaintStatus}" Foreground="{Binding MaintStatus, Converter={StaticResource myColorConverter}}" />
Run Code Online (Sandbox Code Playgroud)

改进

使用这些方法中的任何一种,最好有一个MaintenanceStatus布尔值或枚举值,并将您的样式条件绑定到该值。使用字符串比较是一个坏主意。那只是自找麻烦。这些示例使用字符串比较,因为......好吧......这就是您提供的示例代码中可用的全部内容。