2017-07-05 16 views
0

C#功能在我的XAML我有這樣調用從XAML

<Expander ExpandDirection="Down" Width="Auto" Padding="4"> 
    <Expander.Style> 
     <Style TargetType="Expander"> 
      <Setter Property="IsExpanded" Value="{Binding [email protected]}" /> 
      <Setter Property="Header" Value="{Binding [email protected]}" /> 
      <Setter Property="FontWeight" Value="Bold" /> 

      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True"> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Expander.Style> 
    <ListBox Name="itemsList" 
    ItemsSource="{Binding XPath=UpgradeAction}" 
    ItemTemplate="{StaticResource dtListItemTemplate}" 
    SelectionChanged="listItems_SelectionChanged" 
    Style="{StaticResource styleListBoxUpgradeAction}" 
    ItemContainerStyle="{StaticResource styleListBoxItemUpgradeAction}"> 
    </ListBox> 
</Expander> 

而是頭的值的擴張來自@Name我想有一個C#函數採取在@Name從XAML,做一些事情,然後輸出一個新的字符串作爲Value。因此,這將是這個樣子

<Setter Property="Header" Value="C_Sharp_Function({Binding [email protected]})" /> 

我有我需要使用,但我怎麼在XAML執行呢?很抱歉,如果這聽起來像一個愚蠢的問題的功能,我是新來的C#和XAML 。任何幫助深表感謝!

編輯

下面是一些我在二傳手試圖替換一個多值轉換的東西。我認爲它應該看起來像this中的答案。

<Setter Property="Header" Value="{{Binding [email protected], Converter={StaticResource NameConverter}}, {Binding [email protected], Converter={StaticResource NameConverter}}}" /> 

<Setter Property="Header" Value="{Binding XPath={@Name, @Value}, Converter={StaticResource NameConverter}}" /> 
+0

綁定到你的視圖模型的屬性,並在吸氣的屬性把代碼你的功能 – Milney

+0

我有點認同@Milney,爲什麼不直接在你的viewmodel上使用返回Header的正確值的屬性呢?爲什麼需要這樣做呢? –

+0

但這是轉換器所做的。因此,添加一個轉換器到你的綁定,並做你需要的。 – Evk

回答

1

你所描述的是一個value converter。它看起來有點不同,但邏輯上它是一樣的。

public class NameConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var name = (XmlAttribute)value; 

     // Do anything you like in here. This is just an example. 

     return "something based on " + name.Value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

XAML:

<Window.Resources> 
    <local:NameConverter x:Key="NameConverter" /> 

    <!-- ... --> 

...

<Expander ExpandDirection="Down" Width="Auto" Padding="4"> 
    <Expander.Style> 
     <Style TargetType="Expander"> 
      <Setter Property="IsExpanded" Value="{Binding [email protected]}" /> 
      <Setter 
       Property="Header" 
       Value="{Binding [email protected], Converter={StaticResource NameConverter}}" 
       /> 

如果你想在一個以上的參數來傳遞,您可以使用multi-bindingmulti-value converter

我不確定你爲什麼要做擴展器風格的所有綁定。以下應該同樣適用。我還包括一個更簡單的方法來做IsExpanded觸發器。

public class NameValueConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     var attrName = (XmlAttribute)values[0]; 
     var attrValue = (XmlAttribute)values[1]; 

     // Do stuff here. 

     return attrName.Value + " -> " + attrValue.Value; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 


<Window.Resources> 
    <local:NameValueConverter x:Key="NameValueConverter" /> 
</Window.Resources> 

...

<Expander 
    ExpandDirection="Down" 
    Width="Auto" 
    Padding="4" 
    IsExpanded="{Binding [email protected]}" 
    > 
    <Expander.Style> 
     <Style TargetType="Expander"> 
      <Setter Property="FontWeight" Value="Bold" /> 

      <Style.Triggers> 
       <!-- 
       You can do a regular Trigger instead of a DataTrigger and not 
       have to bother with RelativeSource=Self 
       --> 
       <Trigger 
        Property="IsExpanded" 
        Value="True"> 
        <!-- stuff --> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Expander.Style> 
    <Expander.Header> 
     <MultiBinding Converter="{StaticResource NameValueConverter}"> 
      <Binding XPath="@Name" /> 
      <Binding XPath="@Value" /> 
     </MultiBinding> 
    </Expander.Header> 
</Expander> 

這裏就是Window.Resources雲:

<Window 
    ...x:Class="..." and other attributes... 
    > 
    <Window.Resources> 
     <local:NameConverter x:Key="NameConverter" /> 

     <!-- other resources --> 
    </Window.Resources> 
    <Grid> 
     <!-- The controls that go in the window --> 
    </Grid> 
</Window 
+0

感謝您的回覆。我有一個問題,就是在XAML中究竟是Window.Resources應該去的地方?它是否應該包裝膨脹機?最後,我收到錯誤「在」窗口「類型中找不到」可附加屬性「資源。我應該爲此包含一個命名空間嗎? –

+0

@JohnSmitty我無法診斷XAML我還沒有看到,但我已經更新了我的答案,如果仍有問題,請將您的新XAML添加到您的答案。 –

+0

this似乎工作除了一件事情。在你創建的轉換,我收到錯誤「無法轉換類型'System.Xml.XmlAttribute'的對象來鍵入(字符串)值'System.String'。我試圖將其更改爲value.ToString(),但輸出System.Xml.XmlAttribute而不是實際的字符串。在調試時,我可以在變量「value」中看到一個parm值,但不知道如何獲取它。有任何想法嗎? –