是否可以将值转换器用于窗口属性?

时间:2017-12-18 10:22:01

标签: c# wpf window

我想为窗口的Top和Left属性绑定值。我需要使用valueconverter和valueconverterparameter(确切地说:我需要一个窗口的datacontext),不幸的是我收到错误,我的值转换器还没有找到。

<Window x:Class="Proofing_Tool.wwwww"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:Proofing_Tool"
        xmlns:conv="clr-namespace:Proofing_Tool.ValueConverters"
        mc:Ignorable="d"
        Height="180"
        MinHeight="50" 
        Width="340"  
        SizeToContent="Height" 
        AllowsTransparency="True" 
        WindowStyle="None" 
        Background="{x:Null}"     
        OpacityMask="White"  
        Name="AnomalyWindow"
        Title="Anomaly Details"
        ResizeMode="NoResize" 
        Left="{Binding ., Converter={StaticResource ValueConverterAnomalyToLeft},ConverterParameter={x:Reference Name=AnomalyWindow}}" 
        Top="{Binding ., Converter={StaticResource ValueConverterAnomalyToTop},ConverterParameter={x:Reference Name=AnomalyWindow}}">
    <Window.Resources>
        <conv:ValueConverterAnomalyToLeft x:Key="ValueConverterAnomalyToLeft" />
        <conv:ValueConverterAnomalyToTop x:Key="ValueConverterAnomalyToTop" />
</Window.Resources>

编辑,更新值的问题,以下部分运作良好:

                <TextBlock Grid.Column="1" Text="{Binding  Ano.Name, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
代码背后的代码:

public  Anomaly Ano
{
    get { return _ano; }
    set
    {
        _ano = value;
        NotifyPropertyChanged();
    }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

只更新一次:

         

2 个答案:

答案 0 :(得分:1)

http://www.thejoyofcode.com/WPF_Quick_Tip_Converters_as_MarkupExtensions.aspx

如果您查看该帖子的V3部分,您会发现一个很好的简单优雅的解决方案,它可以让您按照这样做:

Top="{Binding ., Converter={local:MyTopConverter}, ConverterParameter={x:Reference Name=AnomalyWindow}}"

无需在里面定义.Resources就在任何地方。

答案 1 :(得分:1)

  

...不幸的是,我收到错误,我的价值转换器尚未找到。

这是因为设置了LeftTop属性后,创建了资源。

您可以使用元素语法在创建资源后绑定到属性来解决此问题:

<Window x:Class="Proofing_Tool.wwwww"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:Proofing_Tool"
    xmlns:conv="clr-namespace:Proofing_Tool.ValueConverters"
    mc:Ignorable="d"
    Height="180"
    MinHeight="50" 
    Width="340"  
    SizeToContent="Height" 
    AllowsTransparency="True" 
    WindowStyle="None" 
    Background="{x:Null}"     
    OpacityMask="White"  
    Name="AnomalyWindow"
    Title="Anomaly Details"
    ResizeMode="NoResize">
    <Window.Resources>
        <conv:ValueConverterAnomalyToLeft x:Key="ValueConverterAnomalyToLeft" />
        <conv:ValueConverterAnomalyToTop x:Key="ValueConverterAnomalyToTop" />
    </Window.Resources>
    <Window.Left>
        <Binding Path="." Converter="{StaticResource ValueConverterAnomalyToLeft}" ConverterParameter="{x:Reference Name=AnomalyWindow}" />
    </Window.Left>
    <Window.Top>
        <Binding Path="." Converter="{StaticResource ValueConverterAnomalyToTop}" ConverterParameter="{x:Reference Name=AnomalyWindow}" />
    </Window.Top>
</Window>

订单很重要。另一种选择是根据@Clements建议的App.xaml定义资源。