重用DataTemplate

时间:2017-08-24 11:01:07

标签: wpf

我有下面的Expander但是想将DataTemplate用作窗口资源,因为我将在几个地方使用它。我唯一不确定的是带内容的标签“错误/警告:”。这与模板的每次使用都不同。无论如何我仍然可以重复使用相同的模板,但是传入要在此标签中使用的文本?我已经绑定到一个int(在lblOverallInt下面的情况下)所以我不确定如何传入另一个值。

<Expander Header="{Binding Path=lblOverallInt, UpdateSourceTrigger=PropertyChanged}">
    <Expander.HeaderTemplate>
        <DataTemplate>
            <Border Height="24">
                <StackPanel Orientation="Horizontal">
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}">
                            <Style.Resources>
                                <Style TargetType="{x:Type Label}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding}" Value="0">
                                            <Setter Property="Foreground" Value="Yellow"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Converter={StaticResource isZeroConverter}}" Value="False">
                                            <Setter Property="Foreground" Value="Red"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Style.Resources>
                        </Style>
                    </StackPanel.Style>
                    <Label>Errors/Warnings:</Label>
                    <Label Content="{Binding}"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Expander.HeaderTemplate>
</Expander>

1 个答案:

答案 0 :(得分:1)

  

无论如何,我仍然可以重复使用相同的模板,但是传入要在此标签中使用的文本?

您可以绑定到另一个返回“错误/警告:”或其他字符串的属性:

with open('mybundle.txt', 'r') as R:  
    list = []     
    my_data = R.read()  
    my_data = my_data.replace('[','')  
    my_data = my_data.replace(']','')  
    my_data.rstrip('\r\n')  
    my_data = my_data.split('},')  
import ast
for a in my_data:
    rec = a+'}'
    list.append(rec)
m = ast.literal_eval(list[100])  
#Now this works..  
print(m)  
print(m['open'])  

{'volume': 0, 'quoteVolume': 0, 'high': 260.00000522, 'low': 260.00000522, 'date': 1425801600, 'close': 260.00000522, 'weightedAverage': 260.00000522, 'open': 260.00000522}  
260.00000522  

try:  
df_=[ast.literal_eval(x) for x in list]  
df = pd.DataFrame(df_, index=['date'], columns=
["high","low","open","close","volume","quoteVolume","weightedAverage"])      

except EOFError:  
#my_data = pd.DataFrame(list(my_data))  
print(df.head())  

File "<unknown>", line 1 {"date":1503403200,"high":3959,"low":3838.4845461,"open":3881.999999,"close":3887.75413166,"volume":6580841.4708805,"quoteVolume":1683.41702938,"weightedAverage":3909.21640688}}

如果与<Expander Header="{Binding}"> <Expander.HeaderTemplate> <DataTemplate> <Border Height="24"> <StackPanel Orientation="Horizontal"> <StackPanel.Style> <Style TargetType="{x:Type StackPanel}"> <Style.Resources> <Style TargetType="{x:Type Label}"> <Style.Triggers> <DataTrigger Binding="{Binding lblOverallInt}" Value="0"> <Setter Property="Foreground" Value="Yellow"/> </DataTrigger> <DataTrigger Binding="{Binding Path=lblOverallInt, Converter={StaticResource isZeroConverter}}" Value="False"> <Setter Property="Foreground" Value="Red"/> </DataTrigger> </Style.Triggers> </Style> </Style.Resources> </Style> </StackPanel.Style> <Label Content="{Binding YourPropertyName, FallbackValue=Errors/Warnings:}" /> <Label Content="{Binding}"/> </StackPanel> </Border> </DataTemplate> </Expander.HeaderTemplate> </Expander> 的绑定失败,FallbackValue值将显示为默认值,例如,如果应用了模板的YourPropertyName(具有{{的类) 1}} property)缺少此属性。