TextBox FontFamily Binding

时间:2014-04-09 19:44:51

标签: wpf xaml

<TextBox Style="{StaticResource HardBox}" Background="Transparent"  
      TextWrapping="Wrap" x:Name="TextBoxInsertText" 
      FontFamily="{Binding TopPanelViewModel.FontFamily,UpdateSourceTrigger=PropertyChanged}" 
      AcceptsReturn="True" AcceptsTab="True" ScrollViewer.VerticalScrollBarVisibility="Disabled"
      KeyUp="TextBoxInsertText_KeyDown" AllowDrop="True" IsUndoEnabled="True"
      Visibility="{Binding VisibilityPropertiesViewModel.Instance.InsertTextVisibility,Converter={StaticResource BooleanToVisibilityConverter}}">
</TextBox>

<ComboBox Height="20"  SelectedValue="{Binding TopPanelViewModel.FontFamily,UpdateSourceTrigger=PropertyChanged}"
       ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}" Name="Type_Font"   Width="120" Margin="20,0,10,0">
          <ComboBox.ItemTemplate>
             <DataTemplate>
                 <TextBlock FontFamily="{Binding .}" Text="{Binding Source}" FontSize="15"/>
             </DataTemplate>
          </ComboBox.ItemTemplate>
          <ComboBox.ToolTip>
              <TextBlock Text="{Binding LangiageViewModel.CurrentToolTips[25]}"></TextBlock>
          </ComboBox.ToolTip>
</ComboBox>


private System.Windows.Media.FontFamily fontfamily;
    public System.Windows.Media.FontFamily FontFamily
    {
        get
        {
            return this.fontfamily;
        }
        set
        {
            this.fontfamily = value;
            this.PropertyChanged(this, new PropertyChangedEventArgs("FontFamily"));
        }
    }

更改组合框所选项目后,文本框字体系列不会更改。

1 个答案:

答案 0 :(得分:1)

更改FontFamily

中的TextBox约束
<TextBox Style="{StaticResource HardBox}" Background="Transparent"  
  TextWrapping="Wrap" x:Name="TextBoxInsertText" 
  FontFamily="{Binding ElementName="FontFamilyComboBox",Path=SelectedItem}" 
  AcceptsReturn="True" AcceptsTab="True" ScrollViewer.VerticalScrollBarVisibility="Disabled"
  KeyUp="TextBoxInsertText_KeyDown" AllowDrop="True" IsUndoEnabled="True"
  Visibility="{Binding VisibilityPropertiesViewModel.Instance.InsertTextVisibility,Converter={StaticResource BooleanToVisibilityConverter}}">

并为Name

添加ComboBox
<ComboBox Height="20" x:Name="FontFamilyComboBox"  SelectedValue="{Binding TopPanelViewModel.FontFamily,UpdateSourceTrigger=PropertyChanged}"
       ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}" Name="Type_Font"   Width="120" Margin="20,0,10,0">
          <ComboBox.ItemTemplate>
             <DataTemplate>
                 <TextBlock FontFamily="{Binding .}" Text="{Binding Source}" FontSize="15"/>
             </DataTemplate>
          </ComboBox.ItemTemplate>
          <ComboBox.ToolTip>
              <TextBlock Text="{Binding LangiageViewModel.CurrentToolTips[25]}"></TextBlock>
          </ComboBox.ToolTip>
</ComboBox>