2013-04-29 13 views
2

我最近解決了一些幫助在這裏堆棧溢出定型問題...(你可以看到在我的歷史問題/答案)UpdateSourceTrigger沒有被應用到樣式化控制

作爲一個結果,我來了以下面的樣式適用於特定的文本框。

<!--Expanding text box with the max width in the tag property--> 
<!----> 
<!--Will not work with password box!!--> 
<Style TargetType="{x:Type TextBox}" 
     x:Key="ExpandingTextBoxMaxWidthInTag"> 
    <Setter Property="OverridesDefaultStyle" 
      Value="True" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="TextBox"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="1*"> 
          <ColumnDefinition.MaxWidth> 
           <Binding Path="Tag" 
             RelativeSource="{RelativeSource TemplatedParent}"> 
           </Binding> 
          </ColumnDefinition.MaxWidth> 
         </ColumnDefinition> 
         <ColumnDefinition Width="0*" /> 
        </Grid.ColumnDefinitions> 
        <TextBox> 
         <TextBox.Text> 
          <Binding Path="Text" 
            RelativeSource="{RelativeSource TemplatedParent}" /> 
         </TextBox.Text> 
         <TextBox.MaxWidth> 
          <Binding Path="Tag" 
            RelativeSource="{RelativeSource TemplatedParent}" /> 
         </TextBox.MaxWidth> 
        </TextBox> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

(代碼1)

然後我申請在我的代碼,像這樣以文本框...

<TextBox Tag="150" 
     Text="{Binding Path=Username}" 
     Style="{StaticResource ExpandingTextBoxMaxWidthInTag}" /> 

(代碼2)

我必須強調,按照書面,這段代碼按照需要工作。問題出現的時候,我的列表添加UpdateSourceTrigger =的PropertyChanged文本屬性上的綁定...

<TextBox Tag="150" 
     Text="{Binding Path=Username, UpdateSourceTrigger=PropertyChanged}" 
     Style="{StaticResource ExpandingTextBoxMaxWidthInTag}" /> 

(代碼3)

代碼3有一個例外:更新源觸發器不適當觸發。而不是觸發每個擊鍵(就像它沒有應用的樣式),它只會觸發丟失的焦點事件(默認)。

我不能簡單地在樣式中明確應用「PropertyChanged」,因爲我需要將樣式應用於具有關於更新源觸發器的不同需求的一堆TextBox。

如何在我的xaml中獲取指定的更新源觸發器以過濾到我的樣式?

+0

您可以使用控制模板嗎?使用內容控件創建一個新的控件並使用TemplateBinding。 TemplateBinding確實通過了其餘的綁定屬性。 – 2015-09-12 17:20:27

回答

0

你重寫「文本」屬性與你的風格,因此還有「UpdateSourceTrigger = PropertyChanged」部分,不是嗎?只需刪除你的風格中的「文本」屬性綁定,這應該很好地工作。

+0

我不確定我明白你在做什麼?是的,我用我的風格重寫文本屬性,但我用我在XAML中設置的內容覆蓋它,並且XAML包含正確的UpdateSourceTrigger – imdandman 2013-04-29 14:54:48

+0

我忽略了樣式中的標記...我用它來{}標記:) – Mecaveli 2013-04-29 15:06:57