标记中的MultiBinding返回null

时间:2018-03-16 12:57:41

标签: c# wpf xaml multibinding

我正在尝试在按钮标记中传递许多绑定,但我在其标记中收到空结果。这是代码:

<Button Click="ViewDanfSingle"  VerticalContentAlignment="Top" Margin="0 -4 0 0"  Grid.Column="1"  Style="{StaticResource buttonTransparent}">
    <Button.Tag>
        <MultiBinding StringFormat="{}{0} + {1}">
             <Binding Path="XmlSequencia"/>
             <Binding Path="NfesaiChaveAcesso"/>
        </MultiBinding>
    </Button.Tag>
</Button>

以下是我收到Tag值的代码:

private void ViewDanfSingle(object sender, RoutedEventArgs e)
{
     Button btn =  (Button)sender;
     string mytag =  btn.Tag.ToString();
}

模型如下:

public class NfeSaida
{
    #region Constantes

    public const int TamanhoMaximoChaveAcesso = 44;
    public const int TamanhoMaximoRazaoNome = 60;
    public const int TamanhoMaximoNomeFantasia = 60;

    #endregion



        #region Propriedades

    public int NfesaiSequencia { get; set; }
    public int? EmpSequencia { get; set; }
    public int? EdnfeSequenciaDest { get; set; }
    public int? XmlSequencia { get; set; }
    public int UsuSequenciaInclusao
        #endregion

}

但是在我的代码中,Tag属性返回null 我在做什么wrog?我的代码基于on this post

1 个答案:

答案 0 :(得分:1)

您引用的示例仅适用于string类型的属性,例如TextBlock.Text

对于其他类型(Tagobject),您必须使用转换器(this回答使用它)或其他方法。

一种可能性是代理输出使用正在运行的东西;)

<TextBlock Visibility="Collapsed" x:Name="hiddenText">
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0} + {1}">
            <Binding Path="XmlSequencia" />
            <Binding Path="NfesaiChaveAcesso" />
        </MultiBinding>
    </TextBlock.Text></TextBlock>
<!-- use collapsed textblock to generate content for button tag -->
<Button Tag="{Binding Text, ElementName=hiddenText}" Click="ViewDanfSingle" />